Django

Code

Ticket #7217 (closed: wontfix)

Opened 2 months ago

Last modified 2 months ago

ModelForm save method should take an optional dictionary.

Reported by: Peter of the Norse <RahmCoff@Radio1190.org> Assigned to: nobody
Milestone: Component: Uncategorized
Version: SVN Keywords:
Cc: Triage Stage: Unreviewed
Has patch: 0 Needs documentation: 0
Needs tests: 0 Patch needs improvement: 0

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

Change History

05/12/08 12:48:06 changed by jkocherhans

  • status changed from new to closed.
  • needs_better_patch changed.
  • resolution set to wontfix.
  • needs_tests changed.
  • needs_docs changed.

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

05/17/08 08:36:17 changed by Peter of the Norse <RahmCoff@Radio1190.org>

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


Add/Change #7217 (ModelForm save method should take an optional dictionary.)




Change Properties
Action