Can modeladmin edit_handlers be bound to an instance before checking `form.is_valid()` on Create and Edit views?
See original GitHub issueThis is not an issue, more of a feature request to add the ability to further customize form handling in the admin (mainly to support an InlineFormPanel for OneToOneFields & ForeignKeys).
Currently the edit_handler.bind_to_instance
is not called before the ModelFormView subclasses (CreateView and EditView) call form.is_valid()
for put
& post
requests.
This prevents using panels that may customize the form on a “per instance” basis (such as a which related object is chosen).
I currently have to use custom Create & Edit views like this:
class BoundFormMixin:
""" The bound form lets edit handlers customize the form
on a per instance basis.
"""
def get_bound_form(self):
edit_handler = self.get_edit_handler().bind_to_instance(
instance=self.get_instance(),
form=self.get_form(),
request=self.request)
return edit_handler.form
class CreateView(BaseCreateView, BoundFormMixin):
@transaction.atomic
def post(self, request, *args, **kwargs):
form = self.get_bound_form()
if form.is_valid():
return self.form_valid(form)
else:
return self.form_invalid(form)
class EditView(BaseEditView, BoundFormMixin):
@transaction.atomic
def post(self, request, *args, **kwargs):
form = self.get_bound_form()
if form.is_valid():
return self.form_valid(form)
else:
return self.form_invalid(form)
It would be nice if this were built in. I would also like to contribute an InlineFormPanel
to support OneToOneFields and ForeignKeys (similar to a ChooserPanel)
Issue Analytics
- State:
- Created 5 years ago
- Comments:5 (5 by maintainers)
Top Results From Across the Web
Form and field validation - Django documentation
Form validation happens when the data is cleaned. If you want to customize this process, there are various places to make changes, each...
Read more >Use ModelAdmin form in any view - django - Stack Overflow
Now I want to use the MyNonAdmin in a view function to generate a form bound to a particular model, and use that...
Read more >Customising CreateView, EditView and DeleteView
When using CreateView or EditView to create or update model instances, this value will be passed to the edit form, so that any...
Read more >ASP.NET Core Blazor forms and input components
For testing purposes, ignore the following build warning: ... In basic form validation scenarios, an EditForm instance can use declared ...
Read more >Django form processing: Initialization, field access, validation ...
Listing 6-9 Django form instance with initial argument declared in view method ... While you can add these validation checks directly after the...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@frmdstryr Ah, I see what you mean now. To be truly useful, the form/edit_handler binding does need to happen earlier on in the request cycle.
I agree, I think the most backwards-compatible way to do this would be to override
get_form()
, and have that method take care of the binding, as well as form instantiation.We might also want to think about saving a reference to the fully-bound edit handler instance to a view attribute or something, so that it can simply be picked up again in
get_context_data()
, instead of having to recreate/bind everything there.Thanks!