wagtail icon indicating copy to clipboard operation
wagtail copied to clipboard

Aliases don't show nested related items

Open m-gail opened this issue 2 years ago • 1 comments

Issue Summary

When creating an alias of a page, only immediate child elements (e.g. ClusterableModel, Orderable) are displayed in the alias, while the original also shows nested items.

Steps to Reproduce

  1. Start a new project with wagtail start myproject
  2. Edit home/models.py as follows
from django.db import models

from wagtail.models import ClusterableModel, Orderable, Page, ParentalKey
from wagtail.admin.panels import FieldPanel, InlinePanel, MultiFieldPanel


class HomePage(Page):
    pass


# These are not displayed in an alias
class SubItem(ClusterableModel, Orderable):
    parent = ParentalKey(to="Item", related_name="subitems")
    text = models.TextField()

    panels = [
        FieldPanel("text"),
    ]

class Item(ClusterableModel, Orderable):
    parent = ParentalKey(to="SubHomePage", related_name="items")
    text = models.TextField()

    panels = [
        FieldPanel("text"),
        InlinePanel("subitems")
    ]


class SubHomePage(Page):
    content_panels = Page.content_panels + [
        InlinePanel("items")
    ]
  1. Add The following to home/templates/home/sub_home_page.html:
{% for item in page.items.all %}
    {{ item.text }}
    {% for subitem in item.subitems.all %}
        {{ subitem.text }}
    {% endfor %}
{% endfor %}
  1. Create an instance of SubHomePage and an alias pointing to it in the wagtail admin.
  2. The Original SubHomePage shows both Item and SubItem instances, while the alias to it only shows the Item instances.

Any other relevant information. For example, why do you consider this a bug and what did you expect to happen instead?

  • I have confirmed that this issue can be reproduced as described on a fresh Wagtail project: yes
  • Expected behavior: The alias should also display SubItems

Technical details

  • Python version: 3.10.5
  • Django version: 4.0.6
  • Wagtail version: 3.0.1

m-gail avatar Jul 27 '22 12:07 m-gail

After some testing I found, this is because copy_all_child_relations from django-modelcluster only copies the first level of child relations. To copy the items as described some recursive logic would be needed here:

https://github.com/wagtail/wagtail/blob/bcb5b2baacbfec5da0c82b96da3b19dbddf70b5a/wagtail/models/init.py#L1550-L1552

m-gail avatar Jul 28 '22 09:07 m-gail

I can also reproduce the bug using the development version of Wagtail.

Steps to Reproduce

  1. Implement steps 1 and 2 on https://github.com/wagtail/wagtail/issues/8912#issue-1319471225.
  2. Add The following to home/templates/home/sub_home_page.html:
{% for item in page.items.all %}
    {{ item.text }}
    <ul>
        {% for subitem in item.subitems.all %}
            <li>{{ subitem.text }}</li>
        {% endfor %}
    </ul>
{% endfor %}
  1. Create an instance of SubHomePage and an alias pointing to it in the wagtail admin as shown in the following:

bug

As shown in the above figure, the original page shows both Item and SubItem instances, while the alias to it only shows the Item instances.

mehrdadmoradii avatar Aug 11 '22 00:08 mehrdadmoradii

Thanks for adding details and confirming this @mehrdadmoradii

lb- avatar Aug 11 '22 01:08 lb-

Hi @mehrdadmoradii can't produce the issue despite following the exact same steps. Can you please elaborate the steps a bit more?

Akash-Kumar-Sen avatar Aug 14 '22 13:08 Akash-Kumar-Sen

Hi @Akash-Kumar-Sen, thanks for mentioning that. I have followed the steps on https://github.com/wagtail/wagtail/issues/8912#issue-1319471225 with a subtle change on home/templates/home/sub_home_page.html just to highlight the nested text visually. Can I ask which version of Wagtail you are using?

mehrdadmoradii avatar Aug 15 '22 16:08 mehrdadmoradii

I was using the latest main branch for that. But the changes were not reflecting, can you just check and tell me what I am doing wrong? If you are available please let me know then we can use some platform like discord and I will show you the issue live.

Akash-Kumar-Sen avatar Aug 15 '22 16:08 Akash-Kumar-Sen

I was using the latest main branch for that. But the changes were not reflecting, can you just check and tell me what I am doing wrong? If you are available please let me know then we can use some platform like discord and I will show you the issue live.

For sure @Akash-Kumar-Sen, I am on the Slack channel, you can also send me an email to find another way to communicate.

mehrdadmoradii avatar Aug 15 '22 16:08 mehrdadmoradii