Yes, the default value will be set only when the Model metaclass is initialized, and not when creating a new SecretKey instance.
The solution is to make the default value of a callable , in which case the function will be called every time a new instance is created.
def my_random_key(): return randstr(16) class SecretKey(Model): key = CharField(max_length=16, default=my_random_key)
You could, of course, also set the value in the __init__ function of the model, but the callers are cleaner and will still work with standard syntax like model = SecretKey(key='blah') .
Daniel Naab
source share