django.db.utils.OperationalError: foreign key mismatch - "project_projectpage" referencing "auth_user"

django.db.utils.OperationalError: foreign key mismatch - "project_projectpage" referencing "auth_user"

这个问题首先想说什么?

下面是我的 model.py

class ProjectPage(Page):
"""
A Project Page

We access the People object with an inline panel that references the
ParentalKey's related_name in ProjectPeopleRelationship. More docs:
http://docs.wagtail.io/en/latest/topics/pages.html#inline-models
"""
introduction = models.TextField(
    help_text='Text to describe the page',
    blank=True)
image = models.ForeignKey(
    'wagtailimages.Image',
    null=True,
    blank=True,
    on_delete=models.SET_NULL,
    related_name='+',
    help_text='Landscape mode only; horizontal width between 1000px and 3000px.'
)
body = StreamField(
    BaseStreamBlock(), verbose_name="Page body", blank=True
)
subtitle = models.CharField(blank=True, max_length=255)
tags = ClusterTaggableManager(through=ProjectPageTag, blank=True)
date_published = models.DateField(
    "Date article published", blank=True, null=True
    )
#email = models.CharField(max_length=255, blank=True, null=True)
email = models.ForeignKey(User, on_delete=models.PROTECT, to_field='email', null=True)

content_panels = Page.content_panels + [
    FieldPanel('subtitle', classname="full"),
    FieldPanel('introduction', classname="full"),
    ImageChooserPanel('image'),
    StreamFieldPanel('body'),
    FieldPanel('date_published'),
    InlinePanel(
        'project_person_relationship', label="Author(s)",
        panels=None, min_num=1),
    FieldPanel('email'),
    FieldPanel('tags'),
    FieldPanel('added_by', classname="full"),
    FieldPanel('updated_by', classname="full"),
]

search_fields = Page.search_fields + [
    index.SearchField('body'),
]
added_by = models.ForeignKey(People,related_name="project_added_by", null=True, blank=True, on_delete=models.SET_NULL)
updated_by = models.ForeignKey(People,related_name="project_updated_by", null=True, blank=True, on_delete=models.SET_NULL)

created_at = models.DateTimeField(auto_now_add=True, null=True, blank=True)
updated_at = models.DateTimeField(auto_now=True, null=True, blank=True)

如果您想了解有关此问题的更多详细信息,请告诉我上述问题的解决方案!!!!

我不知道,但我认为这里的电子邮件 ID 或其他地方有问题,因为我添加了电子邮件 ID 之后可能会出现此问题。

Django 的 User 模型上 email 的声明是:

email = models.EmailField(_('email address'), blank=True)

This part of the Django documentation 表示,如果您 link 除了默认键之外的任何字段(例如您尝试 link 到 email 的方式),那么该字段必须用 unique=True 声明,但 email 不是。您如何尝试 link 的问题是 User 模型的 email 字段没有索引,因为它没有声明为唯一的。

像这样对 Django 用户模型 link 会更标准(默认情况下,link 是在用户模型的默认键 id 上创建的):

from django.contrib.auth import get_user_model

user = models.ForeignKey(get_user_model(), on_delete=models.PROTECT, null=True)

通过上述声明,在您的模板中,您将通过 {{ page.user.email }} 获得 linked 用户的电子邮件,但我想知道 linking 用户访问页面的架构.请记住,Wagtail 有一个权限结构,您可以使用它为特定页面的用户组分配权限 types/models。