【问题标题】:django.db.utils.OperationalError: foreign key mismatch - "project_projectpage" referencing "auth_user"django.db.utils.OperationalError:外键不匹配 - “project_projectpage”引用“auth_user”
【发布时间】:2020-01-01 06:10:25
【问题描述】:

这个问题首先想说什么?

下面是我的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,之后可能会出现此问题。

【问题讨论】:

  • 你看过this吗?
  • 不,我已经正确地完成了命名约定,所以这不是我认为的问题。@shaikmoed
  • 你可以查看this你能解释一下吗@shaikmoed

标签: python django wagtail


【解决方案1】:

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

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

This part of the Django documentation 表示,如果您链​​接到除默认键之外的任何字段(例如您尝试链接到 email 的方式),则必须使用 unique=True 声明该字段,但 @ 987654327@ 不是。您尝试链接的方式的问题在于,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 有一个权限结构,您可以使用它为特定页面类型/模型的用户组分配权限。

【讨论】:

  • 我想要来自用户表的电子邮件,而不是用户的 id,这就是为什么我在电子邮件中添加了这样的外键,现在它工作正常,但感谢您的回复@Dan Swain
  • 用户表的电子邮件来自外键。您必须编写自定义保存方法来获取选定的 User.email 并将该值保存到电子邮件字段中。为简单起见,丹的答案是最好的。您可以将 ForeignKey 保存到您的 Project 模型并使用 ProjectPage.user.email 这样如果用户更改了他们的电子邮件地址,它也会在您的 ProjectPage 类中更新
猜你喜欢
  • 1970-01-01
  • 2016-12-28
  • 2020-08-14
  • 1970-01-01
  • 2021-03-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-28
相关资源
最近更新 更多