[Solved]-Django model field default from model method

5👍

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.

👤yossi

9👍

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)
👤Rohan

Leave a comment