i need import custom user model third party app, problem app import django's default user model, so:
from django.contrib.auth.models import user i have custom user, this:
from myapp.models import user but want generic solution.
i had tried:
1:
from django.contrib.auth import get_user_model user = get_user_model() output:
django.core.exceptions.appregistrynotready: models aren't loaded yet. 2:
from django.conf import settings django.db.models import get_model app_label, model_name = settings.auth_user_model.split('.') user = get_model(app_label, model_name) output:
django.core.exceptions.appregistrynotready: models aren't loaded yet. 3:
from django.conf import settings user = settings.auth_user_model output:
typeerror isinstance() arg 2 must class, type, or tuple of classes , types specifically need execute code
if isinstance(author, user) , author.is_authenticated(): kwargs['author'] = u"%s <%s>" % (author.get_full_name() or author.username, author.email) elif isinstance(author, six.string_types): kwargs['author'] = author how it? thanks
i think "approach 1" right one, problem can't run get_user_model() until after has been loaded. example, change main code to:
if isinstance(author, get_user_model()) , author.is_authenticated(): kwargs['author'] = u"%s <%s>" % (author.get_full_name() or author.username, author.email) elif isinstance(author, six.string_types): kwargs['author'] = author that slow, since runs get_user_model() each time, should find place keep user model run get_user_model() first time code called. example, if code in method of object, put self._user_model = get_user_model() in constructor.
another option check if author string_types first, , then
try: kwargs['author'] = u"%s <%s>" % (author.get_full_name() or author.username, author.email) except typeerror: # whatever do, if client code passes wrong kind of object. pass
Comments
Post a Comment