以下のようにdjango 1.5ユーザーモデルを拡張しました。データベースに行を挿入すると問題が発生します。私のmodels.pyファイルは以下のようになります。djangoのカスタムユーザーモデル1.5
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は以下のようになります。
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/customizing/#a-full-example)
から上記に続い私は管理者に何かを変更するたびに今私が午前の問題は、私は以下というエラーメッセージを取得しています。
(1452年、 '子行を追加または更新できません:。外部キー制約が失敗する(csiop
をdjango_admin_log
、CONSTRAINT user_id_refs_id_c8665aa
FOREIGN KEY(user_id
)auth_user
(id
)を参照)を')
だから、それは次のようになりますdjango_admin_logテーブルは、常にauth_userモデルへの外部キー参照を必要とします。しかし、私は顧客のユーザーモデルを作成したので、スーパーユーザーを作成すると、ユーザーの詳細は顧客のMyUserテーブルにのみ保存され、auth_userモデルにはエントリが作成されません。
この問題を解決するにはどうすればよいですか?提案してください。
おかげ スリーカンス
設定で 'AUTH_USER_MODEL'を設定しましたか? https://docs.djangoproject.com/en/dev/topics/auth/customizing/#substituting-a-custom-user-model – Ngenator
はい。カスタムMyUserを指すようにAUTH_USER_MODELを設定しました。 – Dev