Code

Opened 4 years ago

Closed 4 years ago

#14167 closed (fixed)

Using Instances in Generic Create/Update/Delete

Reported by: monokrome Owned by: nobody
Component: Generic views Version: 1.2
Severity: Keywords: generic views DRY instance
Cc: Triage Stage: Unreviewed
Has patch: yes Needs documentation: yes
Needs tests: yes Patch needs improvement: yes
Easy pickings: UI/UX:

Description

I would like to proposal that django's create/update/delete methods be able to receive an instance of a model instead of a class reference.

Albeit, it is not difficult to create your own create/update/delete method - using django's generic views would allow an application to easily inherit any additional functionality that might be added to these generic views in the future. It also provides the added benefit of centralizing the work happening to the models, and would help model code follow the DRY principal in many cases.

Here is an example of how a generic view may be used with this idea in mind. In my opinion, this is much more readable than working with the form yourself:

# Assume that we have a model `Job` with a foreign key `user`
from models import Job

def create_job(request):
     """ Create a new job. """
     job_instance = Job()
     job_instance.user = request.user
 
     kwargs = {
         'instance': job_instance,
         'login_required': True,
         'template_name': 'jobs/create.html',
     }
 
     return create_object(request, **kwargs)

Attachments (1)

create_update.py.patch (1.3 KB) - added by monokrome 4 years ago.
A patch that implements the "instance" keyword on the create_object generic view.

Download all attachments as: .zip

Change History (6)

Changed 4 years ago by monokrome

A patch that implements the "instance" keyword on the create_object generic view.

comment:1 Changed 4 years ago by monokrome

  • Has patch set
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

comment:2 Changed 4 years ago by monokrome

I have attached a patch to this ticket which allows this to work as the example code suggests. SmileyChris in #django has also reminded me that this would (obviousl) not be very useful for the delete_object method.

comment:3 Changed 4 years ago by monokrome

  • Needs tests set

comment:4 Changed 4 years ago by mk

  • Needs documentation set
  • Patch needs improvement set

I'm not sure whether working on this ticket makes sense at all with the upcoming class-based generic views (#6735).

I'll still comment on the given patch though.

  • Documentation is missing.
  • Passing instance and model at the same time should be forbidden.
  • The instance should be used to initialize the form on GET requests too, not only with POST.
  • No tests.

comment:5 Changed 4 years ago by russellm

  • Resolution set to fixed
  • Status changed from new to closed

Function-based generic views were deprecated by the introduction of class-based views in [14254]. Class-based views should solve this problem -- just override get_object().

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.