Python django.core.exceptions.FieldError:本地字段';电子邮件';课堂上';用户';与基类';抽象用户';
我在Python django.core.exceptions.FieldError:本地字段';电子邮件';课堂上';用户';与基类';抽象用户';,python,django,django-models,Python,Django,Django Models,我在myreg\u组app的models.py中创建了一个AbstractUser类 from django.contrib.auth.models import AbstractUser from django.db import models from django.utils.html import escape, mark_safe class User(AbstractUser): is_client = models.BooleanField(default=False)
myreg\u组
app的models.py中创建了一个AbstractUser
类
from django.contrib.auth.models import AbstractUser
from django.db import models
from django.utils.html import escape, mark_safe
class User(AbstractUser):
is_client = models.BooleanField(default=False)
is_agency = models.BooleanField(default=False)
is_vendor = models.BooleanField(default=False)
email = models.EmailField(max_length=255, default=None, blank=True, null=True)
class User_Info(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE, primary_key=True)
def __str__(self):
return self.user.name
我还有另一个应用程序notification
,其models.py
如下:
from django.db import models
from swampdragon.models import SelfPublishModel
from .serializers import NotificationSerializer
class Notification(SelfPublishModel, models.Model):
serializer_class = NotificationSerializer
message = models.TextField()
当我运行python manage.py runserver
时,我得到了一个错误
django.core.exceptions.FieldError:类“User”中的本地字段“email”与基类“AbstractUser”中类似名称的字段冲突
但是通知
数据库中没有电子邮件
字段
数据库的原始代码如下所示:
CREATE TABLE "notifications_notification" (
"id" integer NOT NULL PRIMARY KEY AUTOINCREMENT,
"level" varchar(20) NOT NULL,
"actor_object_id" varchar(255) NOT NULL,
"verb" varchar(255) NOT NULL,
"description" text,
"target_object_id" varchar(255),
"action_object_object_id" varchar(255),
"timestamp" datetime NOT NULL,
"public" bool NOT NULL,
"action_object_content_type_id" integer,
"actor_content_type_id" integer NOT NULL,
"recipient_id" integer NOT NULL,
"target_content_type_id" integer,
"deleted" bool NOT NULL,
"emailed" bool NOT NULL,
"data" text,
"unread" bool NOT NULL,
FOREIGN KEY("recipient_id") REFERENCES "reg_group_user"("id") DEFERRABLE INITIALLY DEFERRED,
FOREIGN KEY("target_content_type_id") REFERENCES "django_content_type"("id") DEFERRABLE INITIALLY DEFERRED,
FOREIGN KEY("actor_content_type_id") REFERENCES "django_content_type"("id") DEFERRABLE INITIALLY DEFERRED,
FOREIGN KEY("action_object_content_type_id") REFERENCES "django_content_type"("id") DEFERRABLE INITIALLY DEFERRED
);
这当然是自动生成的
我不明白为什么在没有此类公共字段的情况下会出现字段冲突错误?您不需要在用户
模型上有电子邮件
字段,因为它已经存在于抽象用户
中。
所以只要改变这个:
class User(AbstractUser):
is_client = models.BooleanField(default=False)
is_agency = models.BooleanField(default=False)
is_vendor = models.BooleanField(default=False)
email = models.EmailField(max_length=255, default=None, blank=True, null=True)
为此:
class User(AbstractUser):
is_client = models.BooleanField(default=False)
is_agency = models.BooleanField(default=False)
is_vendor = models.BooleanField(default=False)
您可以继续使用电子邮件
字段,因为它包含在AbstractUser
中
有关其他信息,请参见abstractUser或其父项中也定义了公共字段“电子邮件”吗?这就是错误所告诉您的,它是如何成为一个公共字段的。notification.model
不是您的通知模型。您继承的是AbstractUser,它已经有一个电子邮件字段。这与通知无关。错误在用户中,该用户正在重新定义从抽象用户继承的电子邮件字段。那么,基本上如何覆盖它呢?例如,我有is_staff=models.BooleanField(default=True)
但Django的默认值为False。对于子类,我应该强制谁将其设置为True
?