Opened 16 years ago

Closed 16 years ago

Last modified 16 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: dev
Severity: Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

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()})

Change History (2)

comment:1 by jkocherhans, 16 years ago

Resolution: wontfix
Status: newclosed

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 by Peter of the Norse <RahmCoff@…>, 16 years ago

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

Note: See TracTickets for help on using tickets.
Back to Top