wagtail
wagtail copied to clipboard
Aliases don't show nested related items
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
- Start a new project with
wagtail start myproject
- 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")
]
- 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 %}
- Create an instance of SubHomePage and an alias pointing to it in the wagtail admin.
- 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
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
I can also reproduce the bug using the development version of Wagtail.
Steps to Reproduce
- Implement steps 1 and 2 on https://github.com/wagtail/wagtail/issues/8912#issue-1319471225.
- 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 %}
- Create an instance of SubHomePage and an alias pointing to it in the wagtail admin as shown in the following:
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.
Thanks for adding details and confirming this @mehrdadmoradii
Hi @mehrdadmoradii can't produce the issue despite following the exact same steps. Can you please elaborate the steps a bit more?
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?
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.
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.