Opened 10 years ago

Closed 10 years ago

Last modified 10 years ago

#22768 closed New feature (wontfix)

CreateUpdateView view class

Reported by: adi.andreias@… 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)

createupdateview.py (1.5 KB ) - added by adi.andreias@… 10 years ago.
a working code draft (alpha version)

Download all attachments as: .zip

Change History (6)

by adi.andreias@…, 10 years ago

Attachment: createupdateview.py added

a working code draft (alpha version)

comment:1 by Maxime Turcotte, 10 years ago

Cc: maxime.turcotte@… added

comment:2 by Aymeric Augustin, 10 years ago

I'm a bit skeptical. Django already gets criticized for providing too many generic views.

comment:3 by anonymous, 10 years ago

No need for this to be in core.

comment:4 by Sasha Romijn, 10 years ago

Resolution: wontfix
Status: newclosed

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 Maxime Turcotte, 10 years ago

Cc: maxime.turcotte@… removed
Note: See TracTickets for help on using tickets.
Back to Top