E
E
er122013-12-15 19:41:01
Django
er12, 2013-12-15 19:41:01

How to fix the error when extending the User model?

Here is my code.
This is models.py:

class UserManager(BaseUserManager):
    def create_user(self, email, username, password=None):
        if not email:
            raise ValueError('Users must have an email address')

        user = self.model(
            email=UserManager.normalize_email(email),
            username=username)

        user.set_password(password)
        user.save(using=self._db)
        return user

    def create_superuser(self, email, username, password):
        user = self.create_user(email,
                                password=password,
                                username=username)
        user.is_admin = True
        user.save(using=self._db)
        return user


class User(AbstractBaseUser):
    email = models.EmailField(
        verbose_name='Электропочта',
        max_length=255,
        unique=True,
        db_index=True)
    username = models.CharField(verbose_name='Ник',  max_length=255, unique=True)
    avatar = models.ImageField(verbose_name='Аватар',  upload_to='images/%Y/%m/%d', blank=True, null=True)
    first_name = models.CharField(verbose_name='Имя',  max_length=255, blank=True)
    last_name = models.CharField(verbose_name='Фамилия',  max_length=255, blank=True)
    date_of_birth = models.DateField(verbose_name='День рождения',  blank=True, null=True)
    is_active = models.BooleanField(default=True)
    is_admin = models.BooleanField(default=False)

    objects = UserManager()

    
    REQUIRED_FIELDS = ['username']
    USERNAME_FIELD = 'username'
    def get_full_name(self):
        return '%s %s' % (self.first_name, self.last_name,)

    def get_short_name(self):
        return self.username

    def __unicode__(self):
        return self.email

    def has_perm(self, perm, obj=None):
        return True

    def has_module_perms(self, app_label):
        return True

    @property
    def is_staff(self):
        return self.is_admin

This is admin.py:
class UserAdmin(UserAdmin):
    form = UserChangeForm
    add_form = UserCreationForm


    list_display = ('email', 'username', 'is_admin',)
    list_filter = ('is_admin',) 
    fieldsets = (
        (None, {'fields': ('email', 'username', 'password')}),
        ('Personal info', {'fields': ('date_of_birth', 'first_name', 'last_name', 'avatar')}),
        ('Permissions', {'fields': ('is_admin',)}),
        ('Important dates', {'fields': ('last_login',)}),
    )
    add_fieldsets = (
        (None, {
            'classes': ('wide',),
            'fields': ('email', 'date_of_birth', 'password1', 'password2')}
        ),
    )
    search_fields = ('email',)
    ordering = ('email',)
    filter_horizontal = ()

admin.site.register(User, UserAdmin)
admin.site.unregister(Group)

This is forms.py:
class UserCreationForm(forms.ModelForm):
    password1 = forms.CharField(label='Password', widget=forms.PasswordInput)
    password2 = forms.CharField(label='Password confirmation', widget=forms.PasswordInput)

    class Meta:
        model = User
        fields = ('email', 'username')

    def clean_password2(self):
        password1 = self.cleaned_data.get("password1")
        password2 = self.cleaned_data.get("password2")
        if password1 and password2 and password1 != password2:
            raise forms.ValidationError("Passwords don't match")
        return password2

    def save(self, commit=True):
        user = super(UserCreationForm, self).save(commit=False)
        user.set_password(self.cleaned_data["password1"])
        if commit:
            user.save()
        return user


class UserChangeForm(forms.ModelForm):
    password = ReadOnlyPasswordHashField()

    class Meta:
        model = User

    def clean_password(self):
        return self.initial["password"]

In setting.py I wrote:
AUTH_PROFILE_MODULE = 'news.User'
The error is this:
Exception Type:	FieldError
Exception Value:	
Unknown field(s) (email) specified for User
Exception Location:	C:\Python33\lib\site-packages\django\forms\models.py in __new__, line 221

Answer the question

In order to leave comments, you need to log in

5 answer(s)
S
SilentSokolov, 2013-12-15
@er12

I advise you to start:
`class User(AbstractBaseUser, PermissionsMixin)`
And if you expand/change the user model, then you need to write in the settings :
`AUTH_USER_MODEL = 'news.User'`
Since AUTH_PROFILE_MODULE is a little different...

A
alternativshik, 2013-12-15
@alternativshik

in jang there is also a model named User, probably, there is a cant in the imports?

M
maxaon, 2013-12-15
@maxaon

Have you synced the database? Because the error occurs when the class is created.

A
alternativshik, 2013-12-15
@alternativshik

maybe there is the wrong language for 1 letter in the field name?

A
alternativshik, 2013-12-15
@alternativshik

+ AUTH_PROFILE_MODULE = 'news.User'
would do AUTH_PROFILE_MODULE = 'project.news.User' here....

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question