Which widget can be used in a form to manage data with many-to-many relationships similar to the widgets of the user’s administrative form? The task seems to be standard, it has a solution, but there is no description of it anywhere. Some kind of “conspiracy of silence” may be …
Welcome @MikeWinny !
It’s not just a widget, there’s also some JavaScript and css involved as well.
You’ll want to find the components associated with the css class related-widget-wrapper
, this is going to include
RelatedObjectLookups.js
indjango.contrib.admin.static.admin.js.admin
,related_widget_wrapper.html
indjango.contrib.admin.templates.admin.widgets
,
andforms.css
,responsive.css
andwidgets.css
indjango.contrib.admin.static.admin.css
.
Note: As these are undocumented, there is no assurance that these are going to remain the same across releases. As with any undocumented feature, it can be changed without notice and without going through the normal deprecation process.
As a result, you might want to copy those components into your project and not just reference the existing versions from the Django installation.
Hi, Ken!
Thank you for answer and recomendations.
For a long time I have been trying to figure out how to use the technologies used in the above code, but so far without success. A very sophisticated, but universal code.
In particular, it is unclear how to set the value of the admin_site
parameter when using the User model from the Django kernel, and which one for models from the application.
Unfortunately, I could not find any examples of successful use of this approach,
First, I think I copied some wrong information in my previous response. (The basic idea was right, but I think I identified the wrong javascript file.)
But beyond that, I think I’m kinda lost here and need more context about what it is you’re trying to do.
If you’re copying these components into your own app, I don’t see where the admin_site
parameter needs to play any part in this.
The basic steps would be to find the JavaScript, CSS, and Templates that the admin uses, copy them to your app, and use them in a manner similar to how the admin does it. But there’s no need to implement everything in your code the way that the admin does it.
(I’ve never done this, so I don’t have anything resembling a sample to work from.)
I believe the key point to keep in mind here is that if it’s done correctly, the Django code in the view doesn’t need to change - this is all front-end work with a custom widget. What the HTML form returns should be the same as what a multi-select box returns, making the use of this widget transparent to Django itself.