I'm trying to define in my app models.py a foreign key default value to a custom user model such as:
owner = models.ForeignKey(settings.AUTH_USER_MODEL, null=False,
default=get_user_model().objects.get()[0].pk)
The custom user model is also defined in the same models.py
However, I get the ugly circular import problem in another app models.py when they try to use get_user_model()
:
django.core.exceptions.ImproperlyConfigured: AUTH_USER_MODEL refers to
model 'myapp.MyCustomUserModel' that has not been installed
How do I solve this?
Add a method to get default owner instead of putting it as value.
owner = models.ForeignKey(settings.AUTH_USER_MODEL, null=False,
default=get_default_owner)
#this can be a global method and need not be in same model.
def get_default_owner():
return get_user_model().objects.get()[0]