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

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

问题描述

我已经扩展了django 1.5用户模型,如下所示,当我将任何行插入到数据库中时,我有问题。我的models.py文件如下所示。

I have extended the django 1.5 user model as below and i am having problems when i insert any row into the database. My models.py file looks like below.

class MyUserManager(BaseUserManager):

   def create_user(self, email, password=None):

     if not email:
        raise ValueError('Users must have an email address')

     user = self.model(
        email=MyUserManager.normalize_email(email),
     )

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

   def create_superuser(self, email, password):

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


 class MyUser(AbstractBaseUser):
    email = models.EmailField(
      verbose_name='Email address',
      max_length=255,
      unique=True,
      db_index=True,
    )
    is_active = models.BooleanField(default=True)
    is_admin = models.BooleanField(default=False)

    objects = MyUserManager()

    USERNAME_FIELD = 'email'

    def get_full_name(self):
      # The user is identified by their email address
      return self.email

    def __unicode__(self):
      return self.email

我的admin.py如下所示。

And my admin.py looks like below.

class MyUserAdmin(UserAdmin):
   # The forms to add and change user instances
   form = UserChangeForm
   add_form = UserCreationForm

   # The fields to be used in displaying the User model.
   # These override the definitions on the base UserAdmin
   # that reference specific fields on auth.User.
   list_display = ('email', 'is_admin')
   list_filter = ('is_admin',)
   fieldsets = (
     (None, {'fields': ('email', 'password')}),
     ('Permissions', {'fields': ('is_admin',)}),
     ('Important dates', {'fields': ('last_login',)}),
   )
   add_fieldsets = (
     (None, {
         'classes': ('wide',),
         'fields': ('email', 'password1', 'password2')}
     ),
   )
   search_fields = ('email',)
   ordering = ('email',)
   filter_horizontal = ()

// Now register the new UserAdmin...
admin.site.register(MyUser, MyUserAdmin)
// ... and, since we're not using Django's builtin permissions,
// unregister the Group model from admin.
admin.site.unregister(Group)

我从django教程( https://docs.djangoproject.com/en/dev/topics/auth / customize /#a-full-example

I followed the above from the django tutorial (https://docs.djangoproject.com/en/dev/topics/auth/customizing/#a-full-example)

现在我遇到的问题是每当我在管理员中修改任何内容时,我收到一条错误消息

Now the problem i am having is whenever i modify anything in the admin, i am getting an error message saying below.

(1452,'无法添加或更新子行:外键约束失败( csiop django_admin_log ,CONSTRAINT user_id_refs_id_c8665aa FOREIGN KEY( user_id )参考 auth_user id ))')

(1452, 'Cannot add or update a child row: a foreign key constraint fails (csiop.django_admin_log, CONSTRAINT user_id_refs_id_c8665aa FOREIGN KEY (user_id) REFERENCES auth_user (id))')

它看起来像django_admin_log表总是需要外部引用auth_user模型。但是,因为我创建了一个客户用户模型,当我创建一个超级用户时,用户的详细信息只存储在客户MyUser表中,并且在auth_user模型中没有创建任何条目,这似乎是导致问题的。

So, it looks like django_admin_log table always needs foreign key reference to auth_user model. But because i created a customer user model, when i create a super user the user details are only stored in the customer MyUser table and no entry gets created in the auth_user model, which seems to be causing the issue.

我如何解决这个问题?请建议。

How do i solve this issue ? Please suggest.

感谢
Sreekanth

Thanks Sreekanth

推荐答案

看起来非常像一个(PostgreSQL)数据库错误 - 不是Django错误。 (auth_user)在数据库结构中的ForeignKey约束中被引用(仍然)。这样的事情不应该存在于您的自定义模型中,这被称为MyUser。在这种情况下,引用应该是accounts_myuser或myappname_myuser。

This looks very much like a (PostgreSQL) database error - not a Django error. "auth_user" is (still) referenced in a ForeignKey constraint in your database structure. Such a thing shouldn't exist with your custom model, which is called "MyUser". In this case, the reference should be something like "accounts_myuser" or "myappname_myuser".

我猜,你已经更新了一个已经存在的代码,包括一个旧的数据库。如果您不需要旧的管理员日志,只需删除/删除名为django_admin_log的表,然后运行python manage.py syncdb(或python manage.py migrate,为1.7),让Django重新创建

I guess, you've updated an already existing code including an old database. If you don't need the old admin logs, simply delete/drop the table called "django_admin_log" and then run "python manage.py syncdb" (or "python manage.py migrate" as of 1.7) to let Django re-create this table from scratch.

尽管如此,如果您有其他表格引用了旧Django用户模型,那么您将再次运行同样的问题在其他地方。

Be careful, though: If you have other tables referencing the "old" Django User model, you'll run again into the same kind of trouble in other locations.

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

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