gpt4 book ai didi

python - django.core.exceptions.FieldError : Local field 'email' in class 'User' clashes with field of similar name from base class 'AbstractUser'

转载 作者:行者123 更新时间:2023-12-01 07:34:58 25 4
gpt4 key购买 nike

我在我的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

我还有另一个应用程序通知,其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: Local field 'email' in class 'User' clashes with field of similar name from base class '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
);

这当然是自动生成的

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

最佳答案

您的 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

关于python - django.core.exceptions.FieldError : Local field 'email' in class 'User' clashes with field of similar name from base class 'AbstractUser' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57029010/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com