Wagtail collection as field in page

594 views
Skip to first unread message

Sunil Patel

unread,
Nov 30, 2018, 10:24:01 AM11/30/18
to Wagtail support

Hi,

I am looking for a way to show a list of wagtail collection as a field in a page (just like it showing when you upload an image). A user can select a collection and I can programmatically filter the images to the selected collection. I am still new to wagtail and I am not sure how should I implement this in code.

Thank you in advance for your help.
--
Sunil

Preston Horn

unread,
Nov 30, 2018, 11:04:57 AM11/30/18
to wag...@googlegroups.com
Hey there Sunil! To get the items in a dropdown in the admin, you just need to query from the Collection model and make those your choices. See incomplete code sample below. This will store the collection, allowing you to manipulate and filter the data for page context as needed. Hope that helps!

...
from wagtail.core.models import Collection, Page

class YourPage(Page):
    ...
    # Convert Collections queryset into choices tuple
    COLLECTION_CHOICES = ((c.id, c.name) for c in Collection.objects.all())

    # Add choices tuple to field
    collections = models.CharField(max_length=255, choices=COLLECTION_CHOICES)
    ...

American Documentary, Inc. | amdoc.org
20 Jay Street, Suite 940, Brooklyn, NY 11201

--
You received this message because you are subscribed to the Google Groups "Wagtail support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to wagtail+u...@googlegroups.com.
To post to this group, send email to wag...@googlegroups.com.
Visit this group at https://groups.google.com/group/wagtail.
To view this discussion on the web, visit https://groups.google.com/d/msgid/wagtail/e1b4b520-c62d-4717-b43a-8b46c9848519%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages