Approach for building a Gallery of images (ParentalKey vs StreamField)

Viewed 38

I'm trying to decide between using ParentalKey or StreamField, in this case, with the purpose of adding an image gallery to a page.

We don't need any other information than the image itself (given that the image will be anyway a wagtailimages.Image model's instance, so the alt text is already handled there).

Any thoughts on what is better to make it easier for the editor to work even if maintaining around 50 images per page? And about good practices and code maintainability? Would you prefer a third party package for the image gallery? (even if that blocks you from upgrading to wagtail 4?) Would your opinion change if instead of a single image, we needed some more fields?

Many thanks!

2 Answers

For an image gallery, the first recommendation would be to use the Collections feature. You can get pretty far with the nested collection system and even add extra meta data if needed by adding a model that relates to the collection.

If that is not suitable, ParentalKey/InlinePanel would be my next pick. For simple relationships you get all the benefits of StreamField such as re-ordering, add/remove items but with solid database integrity and usage stats working out of the box.

Only go to StreamField if you need to have optional data set against each image. For example if you have an image list but images could be an Image with a RichText OR just an image.

Unfortunately, managing large sets of images is not great (outside of collections) so you may find you need to build a seperate UI for this. If that ends up being the case you will find migration of data already in model relations being easier to do or maybe not even needed with something like ModelAdmin.

Hope it goes well, be sure to write a blog post about what you end up doing.

I would use the ParentalKey with InlinePanel for that. It shows you all the images as a list in a more compact way than the StreamField. One can reorder this list.

A StreamField is more expandable in the future. If you define each block as StructBlock, you will be able to add whatever you want in the future to these blocks without loosing existing data (also true for the ParentalKey model). You could also add new blocks, like videos or quotes or whatever at any point in the future.

I would not use Collections for image slideshows as you won’t be able to sort the imagas in a collection via the CMS, right? Collections are meant to keep order in the backend, I think.

Related