python - django.db.utils.OperationalError:外键不匹配 - “project_projectpage”引用“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,之后可能会出现这个问题。
解决方案
email
Django模型的声明User
是:
email = models.EmailField(_('email address'), blank=True)
Django 文档的这一部分说,如果您链接到除默认键之外的任何字段(例如您尝试链接到的方式email
),则必须使用 声明该字段unique=True
,但email
不是。您尝试链接的方式的问题是User
模型email
字段上没有索引,因为它没有被声明为唯一的。
像这样链接到 Django 用户模型会更标准(默认情况下,链接是在用户模型的默认键 id 上建立的):
from django.contrib.auth import get_user_model
user = models.ForeignKey(get_user_model(), on_delete=models.PROTECT, null=True)
使用上面的声明,在您的模板中,您将通过 获得链接用户的电子邮件{{ page.user.email }}
,但我想知道将用户链接到页面的架构。请记住,Wagtail 具有权限结构,您可以使用该结构为特定页面类型/模型的用户组分配权限。
推荐阅读
- android - Android Studio Navigation Drawer 如何导航不同的 Fragment?
- oracle - 根据 WHERE 子句从两个结果中获取增加/减少的百分比
- xcode - swiftui list - snap scrolling
- spring-mvc - spring mvc view resolution configuration fail to display jsp page
- python - Python multiple variable optimization with recurrence definition
- javascript - Python 等价于 JS `Symbol`?
- python - 如何在相似性方面比较numpy数组
- faster-rcnn - What is the purpose of a pre-trained network in Faster R-CNN?
- python - Flask-login currentuser becomes anonymous after server restart
- r - 有没有办法改变 R 中数据重要的箱线图颜色