1π
β
The problem was default takes a callable function so whenever an instance of model is instantiated the default function is called. But since I called the function in the second case whenever the server is started and models are loaded it was trying to load model before the model class was created. So the problem comes down to pass a callable function with parameters to default which is not possible as of now. So what I did is this:
def make_random():
return generate_random_unique_code_for_model(['myapp_name', 'mymodel_name'], 'myfield_name')
class mymodel_name(models.Model):
#other fields
myfield_name = models.CharField(default=make_random)
π€dnit13
Source:stackexchange.com