Django model field default from model method Django model field default from model method django django

Django model field default from model method


You can define global method like this:

def create_id():    return os.urandom(12).encode('hex')Class Person(models.Model):   name = models.CharField(max_length = 255)   id = models.CharField(max_length = 255,default = create_id)


I ended up doing this: (removing the self from both)

Class Person(models.Model):    def create_id():        return os.urandom(12).encode('hex')    name = models.CharField(max_length = 255)    id = models.CharField(max_length = 255,default = create_id)

it is working, but i am not sure if this is the best or the right way.