【问题标题】:django.core.exceptions.FieldError: Local field 'email' in class 'User' clashes with field of similar name from base class 'AbstractUser'django.core.exceptions.FieldError:类“用户”中的本地字段“电子邮件”与基类“AbstractUser”中类似名称的字段冲突
【发布时间】:2019-11-23 12:32:39
【问题描述】:

我在my reg_group 应用程序的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:“用户”类中的本地字段“电子邮件”与基类“AbstractUser”中名称相似的字段发生冲突

notification 数据库中没有email 字段。

数据库的原始代码是这样的:

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

我不明白为什么没有这样的公共字段时会出现字段冲突错误?

【问题讨论】:

  • 公共字段“email”是否也在 abstractUser 或其父项中定义?这就是错误告诉你的字面意思
  • 怎么是普通字段
  • 它在notification.model中不存在
  • 不是您的通知模型。您继承自 AbstractUser 已经有一个电子邮件字段
  • 这与通知无关。错误在用户中,它正在重新定义它从抽象用户继承的电子邮件字段。

标签: python django django-models


【解决方案1】:

您不需要在 User 模型中添加 Email 字段,因为它已经存在于 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

【讨论】:

  • 那么基本上你怎么能覆盖它呢?例如,我有is_staff = models.BooleanField(default=True),但 Django 的默认值为 False。我该强迫谁为子类True
猜你喜欢
  • 2013-02-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-26
  • 1970-01-01
  • 1970-01-01
  • 2014-06-13
  • 2014-05-27
相关资源
最近更新 更多