Django中的自定义用户模型 [英] Custom user model in django

查看:71
本文介绍了Django中的自定义用户模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用djangodocs中所述的 django.contrib.auth.models.AbstractUser 创建自定义用户模型:

I want to create a custom user model using django.contrib.auth.models.AbstractUser as stated in the djangodocs:

如果您对Django的用户模型完全满意,并且只想添加一些其他配置文件信息,您可以简单地子类化django.contrib.auth.models.AbstractUser并添加您的自定义配置文件领域.此类提供默认值的完整实现用户作为抽象模型.

If you’re entirely happy with Django’s User model and you just want to add some additional profile information, you can simply subclass django.contrib.auth.models.AbstractUser and add your custom profile fields. This class provides the full implementation of the default User as an abstract model.

因此,我继承了 Users 类中的 AbstractUser 类,并添加了一个字段.但是当我运行 python manage.py syncdb 时,出现以下错误:

So I inherited the AbstractUser class in my Users class and added a field. But when I run the python manage.py syncdb I get the following error:

CommandError: One or more models did not validate:
admin.logentry: 'user' has a relation with model login.users, which has either 
not been installed or is abstract.

我在stackoverflow上遇到了其他问题,但无法解决该错误.这是我的代码:

I went through other questions on stackoverflow but couldn't resolve the error. Here is my code:

models.py

from django.conf import settings
from django.db import models
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.models import AbstractUser
from django.contrib import admin

class Users(AbstractUser):
    college = models.CharField(max_length=40)

admin.site.register(Users, UserAdmin)

admin.py

from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.forms import UserChangeForm, UserCreationForm
from login.models import Users
from django import forms

class UsersChangeForm(UserChangeForm):
    class Meta(UserChangeForm.Meta):
        model = Users

class UsersAdmin(UserAdmin):
    form = UsersChangeForm

    fieldsets = UserAdmin.fieldsets + (
            (None, {'fields': ('college',)}),
    )


admin.site.register(Users, UsersAdmin)

settings.py

INSTALLED_APPS = (
    'forms',
    'login',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
)

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
)

AUTH_USER_MODEL = 'login.users'

我要将用户信息存储在与 auth_user 相同的表中,而不是在新表中.

I want to store the user information in the same table as auth_user and not in a new table.

推荐答案

我在我的一个项目中做到了这一点.我很惊讶地看到您扩展了用户,因为

I did this in one of my project. I was surprised to see that you extended User because the doc says something else :) You can extend Django User model, but if you only want to add new fields (not change its behavior), you should use a OneToOneField.

If you wish to store information related to User, you can use a one-to-one
relationship to a model containing the fields for additional information.

因此,如您在链接中所见,您的代码应类似于:

So, as you can see in the link, your code should look like:

from django.contrib.auth.models import User

class MyUser(models.Model):
    user = models.OneToOneField(User)

    # Or a ForeingKey to the College table?
    college = models.CharField(max_length=40)

    other_data = ...

这篇关于Django中的自定义用户模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆