我在AbstractUser
应用的my reg_group
中创建了一个models.py
类
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'中相似名称的字段冲突
但是email
数据库中没有notification
字段。
数据库的原始代码是这样的:
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
);
这当然是auto-generated
。
我不明白为什么在没有这样的公共字段时为什么会出现字段冲突?
答案 0 :(得分:1)
您不需要在Email
模型上有一个User
字段,因为它已经存在于AbstractUser
中。
因此,只需更改此内容即可:
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)
并且您可以继续使用Email
字段,因为它包含在AbstractUser
中。
有关其他信息,请参见Django Docs