#22768 closed New feature (wontfix)
CreateUpdateView view class
Reported by: | Owned by: | nobody | |
---|---|---|---|
Component: | Generic views | Version: | dev |
Severity: | Normal | Keywords: | CBV class based view update create CreateUpdateView |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | yes | UI/UX: | no |
Description
I think it makes sense to have a Class-based view for both updating and creating model objects.
BaseUpdateView and BaseCreateView differ by just two lines, one in get() and one in post():
https://github.com/django/django/blob/1.7b4/django/views/generic/edit.py#L218
https://github.com/django/django/blob/1.7b4/django/views/generic/edit.py#L195
A CreateUpdateView view would distinguish between create and update based on a slug, pk or similar (configurable?) parameter in url.
There are situations where the view class contains a lot of code logic (like a secondary formset, ajax requests handled along with regular requests, etc.) and having separate classes for create and update would result in duplicate code.
I also think this is a common pattern and deserves to be added to Django core.
Attachments (1)
Change History (6)
by , 10 years ago
Attachment: | createupdateview.py added |
---|
comment:1 by , 10 years ago
Cc: | added |
---|
comment:2 by , 10 years ago
I'm a bit skeptical. Django already gets criticized for providing too many generic views.
comment:4 by , 10 years ago
Resolution: | → wontfix |
---|---|
Status: | new → closed |
Assuming, as you say, that the only difference is those two lines, that would also mean it is trivial to create this yourself: use a BaseUpdateView
based class, and override get_object()
in your class to return None
whenever you want it to behave as a create view, instead of an update view. That will not introduce any code duplication.
I'm closing wontfix, as I then consider this to be something that is trivial to do, by design, outside of Django core.
comment:5 by , 10 years ago
Cc: | removed |
---|
a working code draft (alpha version)