管理面板中一对一用户模型的错误自定义字段
Erroneous custom field of the one-to-one User model in the Admin Panel
我创建了自定义 Subscriber
一对一模型来扩展现有的 Django 用户模型。 (我知道用AbstractUser
更好,但我只是想尝试一下学习)
我可以通过 shell 成功创建新用户,并且我可以编辑(在管理面板中)用户在创建后 具有的自定义字段 。但是,由于错误,我无法在不使用 shell 的情况下在管理面板中创建新用户。
我想 admin.py 有问题吗?
错误:
IntegrityError at /admin/auth/user/add/
UNIQUE constraint failed: app_subscriber.user_id
models.py
from django.db import models
from django.contrib.auth.models import User
from django.db.models.signals import post_save
from django.dispatch import receiver
class Subscriber(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
location = models.CharField(max_length=30, blank=True)
@receiver(post_save, sender=User)
def create_user_subscriber(sender, instance, created, **kwargs):
if created:
Subscriber.objects.create(user=instance)
@receiver(post_save, sender=User)
def save_user_subscriber(sender, instance, **kwargs):
instance.subscriber.save()
admin.py
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin as BaseUserAdmin
from django.contrib.auth.models import User
from . import models
# Define an inline admin descriptor for Subscriber model
# which acts a bit like a singleton
class SubscriberInline(admin.StackedInline):
model = models.Subscriber
can_delete = False
verbose_name_plural = 'subscribers'
# Define a new User admin
class UserAdmin(BaseUserAdmin):
inlines = (SubscriberInline,)
# Re-register UserAdmin
admin.site.unregister(User)
admin.site.register(User, UserAdmin)
不适合创建两个相同的信号(post_save 用户),只创建一个并在其上做所有事情
@receiver(post_save, sender=User)
def create_user_subscriber(sender, instance, created, **kwargs):
if created:
Subscriber.objects.create(user=instance)
@receiver(post_save, sender=User)
def save_user_subscriber(sender, instance, **kwargs):
instance.subscriber.save()
你的错误提示你不能为同一个用户创建 2 个订阅者(UNIQUE 约束)
这解决了你的问题
@receiver(post_save, sender=User)
def create_user_subscriber(sender, instance, created, **kwargs):
# you check if instance are 'subscriber' related name create by OneToOneField , if not create Subcriber model
if created and not hasattr(instance, 'subscriber'):
Subscriber.objects.create(user=instance)
我创建了自定义 Subscriber
一对一模型来扩展现有的 Django 用户模型。 (我知道用AbstractUser
更好,但我只是想尝试一下学习)
我可以通过 shell 成功创建新用户,并且我可以编辑(在管理面板中)用户在创建后 具有的自定义字段 。但是,由于错误,我无法在不使用 shell 的情况下在管理面板中创建新用户。
我想 admin.py 有问题吗?
错误:
IntegrityError at /admin/auth/user/add/
UNIQUE constraint failed: app_subscriber.user_id
models.py
from django.db import models
from django.contrib.auth.models import User
from django.db.models.signals import post_save
from django.dispatch import receiver
class Subscriber(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
location = models.CharField(max_length=30, blank=True)
@receiver(post_save, sender=User)
def create_user_subscriber(sender, instance, created, **kwargs):
if created:
Subscriber.objects.create(user=instance)
@receiver(post_save, sender=User)
def save_user_subscriber(sender, instance, **kwargs):
instance.subscriber.save()
admin.py
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin as BaseUserAdmin
from django.contrib.auth.models import User
from . import models
# Define an inline admin descriptor for Subscriber model
# which acts a bit like a singleton
class SubscriberInline(admin.StackedInline):
model = models.Subscriber
can_delete = False
verbose_name_plural = 'subscribers'
# Define a new User admin
class UserAdmin(BaseUserAdmin):
inlines = (SubscriberInline,)
# Re-register UserAdmin
admin.site.unregister(User)
admin.site.register(User, UserAdmin)
不适合创建两个相同的信号(post_save 用户),只创建一个并在其上做所有事情
@receiver(post_save, sender=User)
def create_user_subscriber(sender, instance, created, **kwargs):
if created:
Subscriber.objects.create(user=instance)
@receiver(post_save, sender=User)
def save_user_subscriber(sender, instance, **kwargs):
instance.subscriber.save()
你的错误提示你不能为同一个用户创建 2 个订阅者(UNIQUE 约束)
这解决了你的问题
@receiver(post_save, sender=User)
def create_user_subscriber(sender, instance, created, **kwargs):
# you check if instance are 'subscriber' related name create by OneToOneField , if not create Subcriber model
if created and not hasattr(instance, 'subscriber'):
Subscriber.objects.create(user=instance)