Use Specific() Method On PageQuerySet To Access Referenced Model's Field Data in Wagtail CMS
Wagtail CMS provides a very handy content tree that really helps in organizing website's content hierarchy. The tree itself is powered by django-treebeard library (https://github.com/django-treebeard/django-treebeard).
As a developer you should be aware of several handy methods that you can execute on any item of a tree (page):
1 2 3 4
The problem with these methods is that it returns all pages as instances of the base
wagtail.wagtailcore.models.Page class, which isn't always what we want. Especially, when we need to access data stored in our retrieved page fields (besides title field, which is always available).
There's a cure for that: use a specific() method on any queryset result, like this:
It'll return results as their most specific type (class) and you can now access page fields without any issues.
The same trick can be used in Django templates too, here's an example:
1 2 3
Hope this helps, enjoy Wagtailing 🐧 !