Opened 14 years ago
Closed 14 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: | no | UI/UX: | no |
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)
Change History (6)
by , 14 years ago
Attachment: | create_update.py.patch added |
---|
comment:1 by , 14 years ago
Has patch: | set |
---|
comment:2 by , 14 years ago
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 by , 14 years ago
Needs tests: | set |
---|
comment:4 by , 14 years ago
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 by , 14 years ago
Resolution: | → fixed |
---|---|
Status: | new → 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().
A patch that implements the "instance" keyword on the create_object generic view.