Proposal: Add a method to ModelAdmin to return the form instance
|Reported by:||igors||Owned by:||nobody|
|Has patch:||yes||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||no|
Sometimes we need dynamic fields in a form, depending on the logged in user or something else. Currently the ModelAdmin class has a get_form() method, that returns a form class to be used in admin. But it would be nice to customize the form instantiation when we are building dynamic forms.
I've attached a patch that adds a get_form_instance() method to ModelAdmin, and it receives the request object. This way we can pass extra parameters to our dynamic form based on some request attributes. For example:
def get_form_instance(self, request, form_class, **kwargs): return form_classe(user=request.user, **kwargs)
I don't think it breaks backwards compatibility and it's not a new feature, so probably could be added to 1.2.
Change History (4)
Changed 6 years ago by igors
comment:1 Changed 6 years ago by igors
- Needs documentation unset
- Needs tests unset
- Patch needs improvement unset