When you have a model that defines a ForeignKey relationship to another model, in the admin Django will display a neat form field that allows to create the ForeignKey model instance right away. It has buttons to edit, add, view or delete an instance of the ForeignKey model. I would like to have this functionality for custom forms as well. However, when I try to set ForeignKeyRawIdWidget for my custom form, it wants an argument for “rel” which I failed to figure out yet. It also wants me to add an admin site. That makes me a bit suspicious. Am I breaking security features by using this outside of the admin context?
Is there another simpler way to add this functionality to custom forms?
Keep in mind that the admin is a contrib module and technically, not a core feature. Using this widget would be considered as using an internal API - which means there’s no guarantee about stability across releases.
I don’t think you’d be breaking any “Django” security features, but I don’t think that doing it this way would be considered good practice.
I’d get past the implementation of how it figures out how to create that link, and just link that icon to the view that generates the appropriate window. There’s very little of that code that you need when you’re working with a known situation. (Unlike the Admin, you don’t need to figure out which field, model, or url to assign to that icon.)
Also keep in mind that the Admin doesn’t do something like create a modal dialog box for this. It opens a whole new window with a model form in it. There’s nothing particularly special about that page. This makes it particularly easy to recreate this yourself.