wagtailwagtail-streamfield

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


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!


Solution

  • 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.