0

I'm trying to write a simple reusable Django app which links into the User model but since this can be replaced I need to link it into AUTH_USER_MODEL.

This is fine within models but I have a formset based on the User so I need a form which I'm trying to populate as follows:

from django.forms import ModelForm
from django.forms.models import inlineformset_factory

from optin.models import UserOptin
from django.conf import settings
#from django.contrib.auth.models import User
USER_MODEL = getattr(settings, 'AUTH_USER_MODEL', None) or \
             'auth.User'

class UserForm(ModelForm):
    class Meta:
        model = USER_MODEL
        fields = '__all__'


class UserOptinForm(ModelForm):
    class Meta:
        model = UserOptin
        fields = '__all__'

UserOptinFormSet = inlineformset_factory(USER_MODEL, UserOptin,
form=UserOptinForm, extra=0)

This generates an error:

AttributeError: 'unicode' object has no attribute '_meta'

This is because USER_MODEL is a string. How do I convert it into the actual model?

1 Answer 1

2

You can use eval() function, all expressions argument parsed inside will be evaluated as a Python expression More info can be found here

USER_MODEL = eval(getattr(settings, 'AUTH_USER_MODEL', None) or 'auth.User') 
# The fallback will raise an Error here

I suggest that you import User from django.contrib.auth.models Because, if AUTH_USER_MODEL is not defined, the fallback will be auth.User, it will raise NameError no module name auth, so you may use the following

from django.contrib.auth.models import User
USER_MODEL = eval(getattr(settings, 'AUTH_USER_MODEL', None) or 'User')
Sign up to request clarification or add additional context in comments.

5 Comments

this looks promising but gives me NameError: name 'auth' is not defined
which option that you choose? the seconds is the one that you shoud use, because there'is no module 'auth' imported
The second is the option I chose
what is the value of AUTH_USER_MODEL in settings.py
I thing AUTH_USER_MODEL is auth.User in your settings.py that's why you have this error. when eval() tries to convert it, it actually doesn't find the auth package

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.