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.
我已经扩展了django 1.5用户模型,如下所示,当我将任何行插入数据库时,我遇到了问题。我的models.py文件如下所示。
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
And my admin.py looks like below.
而我的admin.py如下所示。
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)
I followed the above from the django tutorial (https://docs.djangoproject.com/en/dev/topics/auth/customizing/#a-full-example)
我按照上面的django教程(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, '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
))')
(1452,'无法添加或更新子行:外键约束失败(csiop.django_admin_log,CONSTRAINT user_id_refs_id_c8665aa FOREIGN KEY(user_id)REFERENCES auth_user(id))')
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.
因此,看起来django_admin_log表总是需要对auth_user模型的外键引用。但是因为我创建了一个客户用户模型,当我创建一个超级用户时,用户详细信息只存储在客户MyUser表中,并且在auth_user模型中没有创建任何条目,这似乎导致了这个问题。
How do i solve this issue ? Please suggest.
我该如何解决这个问题?请建议。
Thanks Sreekanth
2 个解决方案
#1
7
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".
这看起来非常像(PostgreSQL)数据库错误 - 而不是Django错误。 “auth_user”(仍然)在数据库结构的ForeignKey约束中引用。您的自定义模型(称为“MyUser”)不应存在此类问题。在这种情况下,引用应该类似于“accounts_myuser”或“myappname_myuser”。
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_admin_log”的表,然后运行“python manage.py syncdb”(或1.7版本的“python manage.py migrate”)让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用户模型,你将在其他位置再次遇到同样的问题。
#2
4
I ran into the same problem. Once you swap out the default Django user model for your own custom one, Django doesn't do anything to clean up old dependencies from the database that are no longer used. There's actually a few of the "auth_*" tables that have a foreign key constraint on the "auth_user" table.
我遇到了同样的问题。一旦你为自己的自定义模型更换了默认的Django用户模型,Django就没有做任何事情来清理数据库中不再使用的旧依赖项。实际上有一些“auth_ *”表在“auth_user”表上有一个外键约束。
Once you've switched over to your new auth models, make sure there's no data you need in any of the "auth_*" and "django_admin_log" tables, drop them, and re-run "manage.py syncdb". You'll notice it doesn't re-create the auth_user table as it's no longer used and the pesky constraint is gone.
切换到新的auth模型后,请确保在任何“auth_ *”和“django_admin_log”表中没有所需的数据,删除它们,然后重新运行“manage.py syncdb”。您会注意到它不会重新创建auth_user表,因为它已不再使用,并且讨厌的约束消失了。
#1
7
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".
这看起来非常像(PostgreSQL)数据库错误 - 而不是Django错误。 “auth_user”(仍然)在数据库结构的ForeignKey约束中引用。您的自定义模型(称为“MyUser”)不应存在此类问题。在这种情况下,引用应该类似于“accounts_myuser”或“myappname_myuser”。
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_admin_log”的表,然后运行“python manage.py syncdb”(或1.7版本的“python manage.py migrate”)让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用户模型,你将在其他位置再次遇到同样的问题。
#2
4
I ran into the same problem. Once you swap out the default Django user model for your own custom one, Django doesn't do anything to clean up old dependencies from the database that are no longer used. There's actually a few of the "auth_*" tables that have a foreign key constraint on the "auth_user" table.
我遇到了同样的问题。一旦你为自己的自定义模型更换了默认的Django用户模型,Django就没有做任何事情来清理数据库中不再使用的旧依赖项。实际上有一些“auth_ *”表在“auth_user”表上有一个外键约束。
Once you've switched over to your new auth models, make sure there's no data you need in any of the "auth_*" and "django_admin_log" tables, drop them, and re-run "manage.py syncdb". You'll notice it doesn't re-create the auth_user table as it's no longer used and the pesky constraint is gone.
切换到新的auth模型后,请确保在任何“auth_ *”和“django_admin_log”表中没有所需的数据,删除它们,然后重新运行“manage.py syncdb”。您会注意到它不会重新创建auth_user表,因为它已不再使用,并且讨厌的约束消失了。