Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document how to add Collections as Foreign Key to a Page #9222

Closed
lukasalexanderweber opened this issue Sep 17, 2022 · 23 comments
Closed

Document how to add Collections as Foreign Key to a Page #9222

lukasalexanderweber opened this issue Sep 17, 2022 · 23 comments

Comments

@lukasalexanderweber
Copy link

Is your proposal related to a problem?

The docs show nicely how to link single Images to a Page. However, a common use case is to store multiple Images to a Page, which could be better managed within a collection instead of having the double work of first adding all images to a collection and then select them 1 by 1 during page creation

Describe the solution you'd like

One should have the possibility to add a Collection as a Foreign Key to a Page, giving the Page access to all Images (or Documents) within this collection. See this or this SO Question.

Describe alternatives you've considered

There are some approaches presented here, but I think the featue is important enough to explain users whats the best way of linking a collection to a page and propose the sollution in the collection docs

@lb-
Copy link
Member

lb- commented Sep 27, 2022

https://docs.wagtail.org/en/stable/reference/pages/model_recipes.html

Would be a good home for this

@hsadia538
Copy link

Hello, I would like to take this as my first contribution to the Wagtail repo. Currently, I am looking into the documents and links mentioned above to understand the issue. Thank you @lb- for letting me know about this one.

@lukasalexanderweber
Copy link
Author

Till now I implemented it using

from django.db import models
from wagtail.core.models import Page, Collection
from wagtail.images.models import Image
from wagtail.admin.edit_handlers import FieldPanel

class PageWithCollection(Page):
    collection = models.ForeignKey(
        'wagtailcore.Collection',
        null=True,
        blank=True,
        on_delete=models.SET_NULL,
        related_name='+',
        verbose_name='Image Gallery'
    )

    content_panels = Page.content_panels + [
        FieldPanel("collection"),
    ]

    def get_context(self, request):
        context = super().get_context(request)
        images = Image.objects.filter(collection=self.collection)
        context['images'] = images
        return context

and in the template

{% load wagtailimages_tags %}
{% for i in images %}
    {% image i width-400 %}
{% endfor %}

@Temidayo32
Copy link
Contributor

@hsadia538 are you working on this?

@nadaHan
Copy link

nadaHan commented Oct 10, 2022

Please assign to me

@hsadia538
Copy link

@Temidayo32 I am no longer working on this.
Feel free to pick this up.

@Temidayo32
Copy link
Contributor

Thanks @hsadia538 .

@lukasalexanderweber
Copy link
Author

The main throwback of my method is that the "collection chooser" dosn't look as nice as the "collection chooser" of e.g. the images panel.

On Images:
Unbenannt
On my custom Page:
Unbenannt2

Can someone give a hint how to access the correct "collection chooser"?

@allcaps
Copy link
Member

allcaps commented Nov 18, 2022

You'd have to register a collection as a snippet to get a modal.

from wagtail.snippets.models import register_snippet
from wagtail.core.models import Collection

register_snippet(Collection)

The downside is that this also gives a "Snippets" menu with a terrible create/update form (treebeard fields).

@ArijeetBanerjee

This comment was marked as duplicate.

@Pritimayasahoo
Copy link

I want to work on this issue please assign me this issue

@gasman
Copy link
Collaborator

gasman commented Feb 6, 2023

Thanks for your interest @Pritimayasahoo! You are welcome to work on this. We do not assign issues outside of the core team - we just ask that people indicate their intention to work on an issue (as you have now done), to avoid duplicating efforts.

For further help in getting started, please see https://dev.to/lb/ten-tasty-ingredients-for-a-delicious-pull-request-hgc - you may also wish to join our #new-contributors channel on Slack.

@jyo031
Copy link

jyo031 commented Mar 14, 2023

I want to work on this issue. Please assign me this issue.

@gasman
Copy link
Collaborator

gasman commented Mar 14, 2023

Thanks for your interest @jyo031! As I said above, we do not assign issues - anyone is free to work on any issue. Now that you've expressed an interest, other people may choose to hold off on picking this up, but they are not required to.

@RohanJnr
Copy link

Hey, I am interested in contributing to this but I have a little curious on why the CollectionMember() model does not have backward compatability with the collection foreign key ? It would be useful to fetch all collection items (images, docs) from the collection instance itself.

@moses966
Copy link

I am competely new here. Does this issue require to explain the code above or it is about something else?
If it is about something else, please help me understand what it could be.

@lb-
Copy link
Member

lb- commented Jul 24, 2023

@moses966 this issue is about adding documentation with an example of adding a Collection to a Page.

@moses966
Copy link

Thank you please

@NXPY123
Copy link
Contributor

NXPY123 commented Sep 11, 2023

Does this have to be added to the docs or the guide?

@NXPY123
Copy link
Contributor

NXPY123 commented Sep 11, 2023

The solution mentioned by @lukasalexanderweber works well and I can add that to the docs/guide.

@allcaps
Copy link
Member

allcaps commented Sep 12, 2023

I'm not sure if this is something that must be documented. The individual parts of this solution are already documented. And there are infinite many smart things that you can do with Django and Wagtail. We can not add all smart things to the documentation. The documentation should not turn into a big collection of implementation snippets.

Maybe we can improve specific parts of the documentation? I think ForeignKeys, Collections, Snippets, Choosers and get_context features are sufficiently documented. And if not, we might append them.

Contributing to the Wagtail community can also be writing a blogpost. Or write a StackOverflow QA. This way we 'document' the feature too.

I know my opinion is debatable. And am happy to be corrected on this one. What section of the documentation lists implementation recepies? Because I can't think of any.

I'd also like to spare everybody the effort of creating documentation that might not get merged.

@lb-
Copy link
Member

lb- commented Sep 12, 2023

Happy to align on this. I'll close this for now.

Anyone's free to write a blog post and then link in our third party tutorial section.

@lb- lb- closed this as completed Sep 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests