Code

Opened 6 years ago

Closed 6 years ago

Last modified 6 years ago

#7217 closed (wontfix)

ModelForm save method should take an optional dictionary.

Reported by: Peter of the Norse <RahmCoff@…> Owned by: nobody
Component: Uncategorized Version: master
Severity: Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

The only time we use save(commit=False) is when there are fields in the model that aren’t in the form. There are a lot of places where it looks like

form = SomeForm(request.POST)
instance = form.save(commit=False)
instance.other_field = "some value"
instance.another_field = calulated_value()
instance.save()

There are literally dozens of places where I have that, but no other places that use commit=False. It would be better if there was a way to pass a dictionary of values like form.save(extra = {"other_field": "some value", "another_field": calculated_value()})

Attachments (0)

Change History (2)

comment:1 Changed 6 years ago by jkocherhans

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Resolution set to wontfix
  • Status changed from new to closed

You can create your instance before passing it into a ModelForm. I use this method almost exclusively.

obj = MyModel(
    other_field = "some value"
    another_field = calulated_value()
)
form = SomeForm(request.POST, instance=obj)
new_obj = form.save()

comment:2 Changed 6 years ago by Peter of the Norse <RahmCoff@…>

It took me a while to get back to my Django project, but you’re right, this is a much better way.

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.