如何将类继承从django.db.models更改为wagtail.core.models.Page

时间:2019-08-01 15:45:32

标签: wagtail wagtail-streamfield

我决定将CMS添加到我的django项目(Wagtail CMS)中。这是一个博客,并且变得有些庞大和复杂。在浏览了wagtail文档之后,据记录,所有页面模型都应继承自wagtail.core.models.Page。

我添加CMS的主要原因是为了获得额外功能。但是在添加流场时,我无法使其显示在前端。

我尝试将类BlogPost的继承从model.Model更改为Page,但是最终在迁移过程中遇到了一个{new}(自动生成)字段page_ptr,该字段需要unique=True和设置blank=Falsenull=False后,无法提供唯一的字段值。它只是一直在抱怨;默认值不是唯一的,或者默认值不是以10为基数的可转换文字(类型:int)。

我怀疑没有显示块引用,因为首先,该模型不是从wagtail.core.models.Page继承的。因此,我尝试更改类BlogPost的继承

从:

class BlogPost(model.Models):
    ...
    ...

收件人:

class BlogPost(Page):
    ....
    ....

现在,这些是我当前的文件状态:

models.py

class BlogPost(models.Model):
    """
    This is the main model of the blog post. It covers everything that describes the blog post. They include,
    Display pictures for the index,
    Display pictures for the blog post header image,
    Different pictures included in the blog post,
    quoted in the blog post,
    resizable image engine to resize the display image at the index page, etc.
    """
    blog_post_author = models.ForeignKey(Author,
                                         null=True,
                                         on_delete=models.CASCADE)
    blog_post_category = models.ForeignKey(BlogCategory,
                                           null=True,
                                           on_delete=models.CASCADE)
    blogpost_title = models.CharField("Title",
                                      max_length=200,
                                      help_text="If necessary, make minimal use of ?s, !s, \\s and :s",
                                      null=True,
                                      blank=True,)
    blog_post_body = StreamField([
        ('Paragraph', blocks.TextBlock()),
        ('Full_Width_Image', ImageChooserBlock()),
        ('Pararaph_and_right_image', ParagraphAndRightImage()),
        ('Paragraph_and_left_image', ParargraphAndLeftImage()),
        ('Quote', QuoteAndAuthorBlock()),
    ], null=True, blank=True)

    blog_post_catch_summary = models.TextField(verbose_name="Catch-phrase summary. AS SHORT AS POSSIBLE!",
                                               null=True,
                                               blank=True,
                                               help_text="This is a catchy summary shown on the index page"
                                               )
    blogpost_date_and_time = models.DateTimeField("Date",
                                                  null=False,
                                                  auto_created=True,
                                                  default=django.utils.timezone.now,
                                                  )
    blogpost_last_edited = models.DateTimeField("Last Edited",
                                                auto_now=True,
                                                blank=True,
                                                null=True
                                                )

    """
    Cover blog post picture details
    """
    blog_post_image_for_top = models.ImageField("Blog Post Image image for top >>> COMPULSORY! IF OMITTED, BLOG POST BECOMES INACCESSIBLE.",
                                                help_text="Blog post header picture.RECOMMENDED RESOLUTION: 1920 x 720.",
                                                upload_to=code_scratchpad.save_location_for_blog_post_related_files,
                                                null=True,
                                                blank=True,
                                                )
    """
    Blog Paragraphs 1 & 2
    """
    blogpost_paragraph_1 = models.TextField("Paragraph 1",
                                            blank=True,
                                            null=True
                                            )
    blogpost_paragraph_2 = models.TextField("Paragraph 2",
                                            blank=True,
                                            null=True
                                            )
    blog_post_image_1 = models.ImageField("Blog Post Image 1",
                                          help_text="RECOMMENDED RESOLUTION: 400 x 300",
                                          upload_to=code_scratchpad.save_location_for_blog_post_related_files,
                                          null=True,
                                          blank=True,
                                          )
    blog_post_image_1_caption = models.CharField("Blog Post Image 1 caption",
                                                 blank=True,
                                                 null=True,
                                                 max_length=70,
                                                 )

    """
    Blog Paragraph 3 & 4
    """
    blogpost_paragraph_3 = models.TextField("Paragraph 3",
                                            blank=True,
                                            null=True
                                            )
    blogpost_paragraph_4 = models.TextField("Paragraph 4",
                                            blank=True,
                                            null=True
                                            )

    """
    Blog Quote
    """
    blog_post_quote = models.TextField("Quote",
                                       null=False,
                                       blank=True
                                       )
    blog_post_quote_author = models.TextField("Quote's Author",
                                              null=False,
                                              blank=True
                                              )

    """
    Blog Paragraph 5
    """
    blogpost_paragraph_5 = models.TextField("Paragraph 5",
                                            blank=True,
                                            null=True
                                            )
    blog_post_image_3 = models.ImageField("Blog Post Image 3",
                                          upload_to=code_scratchpad.save_location_for_blog_post_related_files,
                                          help_text="RECOMMENDED RESOLUTION: 1200 x 900",
                                          null=True,
                                          blank=True,
                                          )

    """
    Conclusion Details
    """
    blogpost_conclusion = models.TextField("Conclusion Title",
                                           blank=True,
                                           null=True,
                                           )
    blogpost_conclusion_paragraph_1 = models.TextField("Conclusion Paragraph 1",
                                                       blank=True,
                                                       null=True
                                                       )
    blogpost_conclusion_paragraph_2 = models.TextField("Conclusion Paragraph 2",
                                                       blank=True,
                                                       null=True
                                                       )

    """
    Podcast
    """
    blog_post_podcast = models.FileField(upload_to=code_scratchpad.save_location_for_blog_post_related_files,
                                         blank=True,
                                         null=True,
                                         help_text="Upload a podcast audio file for the blog post"
                                         )

    """
    Index page Display Picture
    """
    blog_post_resizable_image = models.ImageField("Blog post index page resizable image. THIS IS COMPULSORY!",
                                                  upload_to=code_scratchpad.save_location_for_blog_post_related_files,
                                                  null=True,
                                                  blank=False,
                                                  editable=True,
                                                  help_text="Catch Phrase Image",
                                                  )
    image_height = models.PositiveIntegerField(null=True,
                                               blank=True,
                                               editable=True,
                                               help_text="Image height (in pixels{px})"
                                               )
    image_width = models.PositiveIntegerField(null=True,
                                              blank=True,
                                              editable=True,
                                              help_text="Image width (in pixels{px})"
                                              )

    def __unicode__(self):
        return "{0}".format(self.blog_post_resizable_image)

    # noinspection PyMethodOverriding
    def save(self):
        """ Save function for the resizable image to be displayed at the index"""
        if not self.blog_post_resizable_image:
            return

        super(BlogPost, self).save()
        blog_post_resizable_image = Image.open(self.blog_post_resizable_image)
        (width, height) = blog_post_resizable_image.size
        size = (self.image_width, self.image_height)
        blog_post_resizable_image = blog_post_resizable_image.resize(size, Image.ANTIALIAS)
        blog_post_resizable_image.save(self.blog_post_resizable_image.path)

    """
    Blog display publish date
    """
    blog_post_publish_date = models.DateField("Publish Date", blank=True, null=False, default=django.utils.timezone.now)

    panels = [
        FieldPanel('blog_post_author'),
        FieldPanel('blog_post_category'),
        FieldPanel('blog_post_catch_summary'),
        FieldPanel('blog_post_image_for_top'),
        FieldPanel('blogpost_title'),
        StreamFieldPanel('blog_post_body'),
        FieldPanel('blog_post_podcast'),
        FieldPanel('blogpost_date_and_time'),
        FieldPanel('blog_post_resizable_image'),
        FieldPanel('image_height'),
        FieldPanel('image_width'),
        FieldPanel('blog_post_publish_date'),
    ]

    objects = models.Manager()  # default django compiled manager
    positions = Positions()

    def __str__(self):
        return self.blogpost_title

blocks.py

class QuoteAndAuthorBlock(blocks.StructBlock):
    quote = blocks.RichTextBlock()
    quote_author = blocks.CharBlock()

    class Meta:
        template = 'ixorabloom_blog/BlogPost/quote_and_author.html'
        icon = 'user'


class ParagraphAndRightImage(blocks.StructBlock):
    paragraph = blocks.RichTextBlock()
    right_image = ImageChooserBlock()

    class Meta:
        template = ''


class ParargraphAndLeftImage(blocks.StructBlock):
    paragraph = blocks.RichTextBlock()
    left_image = ImageChooserBlock()

    class Meta:
        template = ''

quote_and_author.html

{% load wagtailcore_tags %}
                    <blockquote class="blockquote">
                        <p>{{ value.quote }}</p>
                        <footer class="blockquote-footer">{{ value.quote_author }}</footer>
                    </blockquote>

blog_post.html

                    {# blockquote #}

                    {% include_block page.blog_post_body %}

                    {# endblockquote #}

我希望在Wagtail管理员中输入内容后,会在前端看到块引用,但这不会发生。

老实说,我现在不知道该怎么办。如果唯一的选择是创建另一个“页面”模型来处理博客文章,那么这几乎会适得其反,因为当前博客文章的数据库及其所有数据都将被没收。

谢谢。

0 个答案:

没有答案