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):
And my admin.py looks below.
class MyUserAdmin(UserAdmin):
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
source share