简体   繁体   中英

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

what this question want to say first of all?

And here below is my 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)

What is solution for above problem let me know if you want more detail for this question!!!!

I don't know but i think problem here in email id or somewhere else because i added email id after that this problem may arise.

The declaration for email on Django's User model is:

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

This part of the Django documentation says that, if you link to any field besides the default key (such as how you're trying to link to email ), then that field would have to be declared with unique=True , but email isn't. The problem with how you're trying to link is that there is no index on the User model's email field because it isn't declared as unique.

It would be more standard to link to the Django user model like this (by default, the link is made on the user model's default key, the id):

from django.contrib.auth import get_user_model

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

With the above declaration, in your template you would get the linked user's email by {{ page.user.email }} , but I wonder about the architecture of linking a user to a page. Remember that Wagtail has a permissions structure that you can use to assign permissions to groups of users for specific page types/models.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM