django中的自定义用户模型

时间:2014-01-18 03:43:31

标签: python django model-view-controller django-models

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

  

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

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

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上遇到了其他问题,但无法解决错误。这是我的代码:

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相同的表中,而不是存储在新表中。

3 个答案:

答案 0 :(得分:8)

我在我的一个项目中这样做了。我很惊讶地看到你扩展了用户因为the doc says something else :)你可以扩展Django用户模型,但如果你只想添加新字段(不改变其行为),你应该使用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.

因此,正如您在链接中看到的,您的代码应如下所示:

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 = ...

答案 1 :(得分:3)

通过使用get_user_model()(获取项目中活动的用户模型。在django.contrib.auth中定义)和AUTH_USER_MODEL(用于引用用户模型)而不是使用get_user_model()来保持代码的通用性是一种很好的做法。直接引用用户模型。

from django.conf import settings

class MyUser(models.Model):
    user = models.OneToOneField(settings.AUTH_USER_MODEL)

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

    other_data = ...

答案 2 :(得分:1)

尝试在 models.py

中删除此行

admin.site.register(Users, UserAdmin

我发现了你的错误,你的 INSTALLED_APPS 顺序错误,应该是下一个:

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    #'django.contrib.sites',
    #'django.contrib.messages',
    'django.contrib.staticfiles',
    # Uncomment the next line to enable the admin:
    'django.contrib.admin',
    # Uncomment the next line to enable admin documentation:
    # 'django.contrib.admindocs',
    'login',
    'forms',
)

AUTH_USER_MODEL = "login.User"
  

为什么?

查看source code 管理员会使用用户模型,所以也许您可以这样想:

但我的用户模型是 login 应用,这个应用在管理员应用之前,因此在管理员之前安装了 login ,为什么呢不起作用?这是因为 django.contrib.auth 是可交换的(在您的情况下可以替换另一个模型已经可以通过login.User进行交换),因此您创建了一个新的用户模型,因此django必须更改其user建模到新模型,然后你必须安装其余部分。

How must be at the moment of run syncdb:


1.- django.contrib.auth is installed, then this see if has been swapped, if yes it adds the new field or override completely the User Model (in your case only you add a field).

2.- admin, contentypes.... your apps, are installed and all works.

How you had it:

1.- login is installed (where the new User Model was, you put the AUTH_USER_MODEL in settings)

2.- Then forms.

3.- Then django tries to install the admin app but it can't because auth_user table hasn't been added that's because django.contrib.auth was after admin, therefore there is no User Model, this one isnt's swapped and this throws the error  **admin.logentry: 'user' has a relation with model login.users, which has either 
not been installed or is abstract.**

因此,首先您必须先安装django.contrib.auth应用,然后安装所有依赖的应用。