I have created a custom widget which has a link to some ajax functionality. In order to get this to work, I need to pass the pk of the instance I am currently editing. It seems that widgets have no way of accessing the current model instance (for good reason!) so I was wondering how I would get this information? Would I have to obtain it from the uri or is there a handy method I am overlooking which will give me what I need.
Thanks
You can override the
__init__method of the widget and pass it the the form instance. From the form instance you can get the pk if exists.In this answer, they bind the form_instance to the widget on the init of the form.
django – how can I access the form field from inside a custom widget
Here is another question post talking about custom widgets accessing form instances.
Country/State/City dropdown menus inside the Django admin inline