Opened 8 years ago

Closed 8 years ago

Last modified 8 years ago

#9769 closed (invalid)

ManyToManyField Widget: saving object does not work

Reported by: sindrero 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

In our application, we are unable to save a ManyToManyField, using the standard webform widgit. MultipleSelectChecbox also does not work. The scalar_properties, remain an empty list when objects are added through the webform. All other fields are properly saved.

When examining the request.POST object, I se that a numeric value is returned. However request.POST includes only ONE numeric value, when it SHOULD have returned several. The number returned, is always equal to the numbering of the last selected element in the list. (The last element in the list is named 1, the second last is named 2 an so on.) There seems to be no way of extracting all objects that are actually selected out from the returned number.

Example code (simplification of our aplication):

in models.py, we have somthing like:

...
class Calculation(models.Model):
    ...
    scalar_properties = fields.ManyToManyField("ScalarProperty")
    ...

class ScalarProperty(models.Model)
    ...

in views.py we have:

def edit(request, ...):
    instance = ... # The original object, before editing
    if request.POST.method == "GET":
        # Create form:
        ....
    elif request.POST.method == "POST":
        # Save object from form:
        calc = forms.CalculationForm(request.POST, instance=instance).save(commit=False) 
        ...
        calc.save()
    # Redirect:
    ...

...

Change History (2)

comment:1 Changed 8 years ago by Alex Gaynor

Needs documentation: unset
Needs tests: unset
Patch needs improvement: unset
Resolution: invalid
Status: newclosed

Because you are using commit=False you need to use save_m2m() as documented.

comment:2 Changed 8 years ago by (none)

milestone: post-1.0

Milestone post-1.0 deleted

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