Opened 14 months ago

Closed 14 months ago

Last modified 14 months ago

#22768 closed New feature (wontfix)

CreateUpdateView view class

Reported by: adi.andreias@… Owned by: nobody
Component: Generic views Version: master
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@… 14 months ago.
a working code draft (alpha version)

Download all attachments as: .zip

Change History (6)

Changed 14 months ago by adi.andreias@…

a working code draft (alpha version)

comment:1 Changed 14 months ago by maxocub

  • Cc maxime.turcotte@… added
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

comment:2 Changed 14 months ago by aaugustin

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

comment:3 Changed 14 months ago by anonymous

No need for this to be in core.

comment:4 Changed 14 months ago by erikr

  • Resolution set to wontfix
  • Status changed from new to 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 Changed 14 months ago by maxocub

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