ixc / wagtail-instance-selector

A widget for Wagtail's admin that allows you to create and select related items
MIT License
54 stars 17 forks source link

Get context of instance selector #19

Open tbrlpld opened 3 years ago

tbrlpld commented 3 years ago

Not an issue, just a question:

I was wondering if there is a way to get the context in which the instance selector panel/modal is opened?

In my use case I would like to filter the instances displayed in the admin based on the current page in which the selector panel is opened. The model admin already comes with the filtering ability. All I would need from the context is the current pages id so that I can use that in the selectors get_instance_selector_url.

markfinger commented 3 years ago

I don't think there's a built-in or easy way to do it.

I think the best approach would be to modify the widget's rendering to add an extra parameter onto the model-admin's url, then modify your model admin's index view to use the value in the param.

Kinda related: #7

tbrlpld commented 3 years ago

Thanks @markfinger.

Do you have a pointer on how the widget would get access to the context of the page that is being edited? I am assuming that the page edit interface is a Django edit view... not sure if that is correct.

markfinger commented 3 years ago

I have a vague memory of looking into something similar a year or two back and found that the usual APIs for Wagtail's forms only provide minimal data to the widgets (eg: only a field's value, not the root object itself). That being said, I wouldn't be surprised if there's some undocumented method that could be used.

Might be worth asking in the Wagtail Slack how to get access to the root object (eg: a page or other model instance) from a content panel.

tbrlpld commented 3 years ago

Thanks @markfinger. I will do that 👍