1

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.

Chirag Kanzariya
  • 377
  • 1
  • 4
  • 11

1 Answers1

1

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.

Dan Swain
  • 2,910
  • 1
  • 16
  • 36
  • i want email from user's table instead of id of user that's why i have added such foreign key with email and its working fine now but thank you for reply@Dan Swain – Chirag Kanzariya Aug 28 '19 at 13:22
  • 1
    The email form the User table is from a foreign key. You'd have to write a custom save method to get the selected User.email and save that value into the email field instead. For simplicity, Dan's answer is the best. You can save a ForeignKey to your Project model and use ProjectPage.user.email and this way if the user changes their email address, it's also updated in your ProjectPage class – Kalob Taulien Aug 28 '19 at 15:18