User model in django 1.5

I have expanded the django 1.5 user model as shown below and I am having problems when I insert any row into the database. My models.py file is as follows.

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 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 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 )

Now the problem I am facing is whenever I change something in the admin, I get the error message below.

(1452, "Unable to add or update a child row: foreign key constraint failed ( csiop . django_admin_log , CONSTRAINT user_id_refs_id_c8665aa FOREIGN KEY ( user_id ) LINKS auth_user ( id )"))

So it looks like the django_admin_log table always needs a foreign key reference to the auth_user model. But since I created a custom client model, when I create a superuser, the user data is only stored in the client’s MyUser table, and no record is created in the auth_user model, which seems to cause the problem.

How can I solve this problem? Please suggest.

Thanks Srikant

+6
source share
2 answers

This is very similar to a PostgreSQL database error, not a Django error. "auth_user" (as before) refers to a ForeignKey constraint in your database structure. Such a thing should not exist with your custom model called MyUser. In this case, the link should be something like "accounts_myuser" or "myappname_myuser".

I think you updated the existing code, including the old database. If you don't need the old admin logs, just delete / drop the table named "django_admin_log" and then run "python manage.py syncdb" (or "python manage.py migrate" starting from 1.7) to restart Django this table with zero.

Be careful: if you have other tables that reference the "old" Django User model, you will again encounter the same problems elsewhere.

+7
source

I ran into the same problem. After you change the default Django user model for your own, Django does nothing to clear old dependencies on a database that is no longer in use. In fact, there are several auth_ * tables that have a foreign key constraint in the auth_user table.

As soon as you switch to the new auth models, make sure that you don’t need any data in any of the auth_ * and django_admin_log tables and run them again in manage.py syncdb. You will notice that it does not recreate the auth_user table, as it is no longer used, and the annoying restriction is gone.

+4
source

All Articles