Skip to content

Commit

Permalink
Fixed ManyToMany form field save handling
Browse files Browse the repository at this point in the history
  • Loading branch information
tabo committed Mar 22, 2012
1 parent 8de1054 commit 688c2f4
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
2 changes: 2 additions & 0 deletions CHANGES
Expand Up @@ -21,6 +21,8 @@ Release 2.0 (XXX XX, 2012)
* Removed the Benchmark (tbbench) and example (tbexample) apps.
* Fixed url parts join issues in the admin.
* Fixed: Now installing the static resources (fixed by omad)
* Fixed ManyToMany form field save handling (fixed by Alexei Vlasov and
Alejandro Peralta)

Release 1.61 (Jul 24, 2010)
---------------------------
Expand Down
21 changes: 15 additions & 6 deletions treebeard/forms.py
Expand Up @@ -2,6 +2,7 @@

from django.forms.models import model_to_dict, ErrorList, BaseModelForm
from django import forms
from django.db.models.query import QuerySet
from django.utils.translation import ugettext as _


Expand Down Expand Up @@ -129,19 +130,27 @@ def save(self, commit=True):
reference_node_id = 0
if '_ref_node_id' in self.cleaned_data:
reference_node_id = self.cleaned_data['_ref_node_id']
position_type = self.cleaned_data['_position']

# delete auxilary fields not belonging to node model
del self.cleaned_data['_ref_node_id']
del self.cleaned_data['_position']
# delete auxilary fields not belonging to node model
del self.cleaned_data['_ref_node_id']

if '_position' in self.cleaned_data:
position_type = self.cleaned_data['_position']
# delete auxilary fields not belonging to node model
del self.cleaned_data['_position']

if self.instance.pk is None:
cl_data = {}
for field in self.cleaned_data:
if not isinstance(self.cleaned_data[field], (list, QuerySet)):
cl_data[field] = self.cleaned_data[field]
if reference_node_id:
reference_node = self.Meta.model.objects.get(
pk=reference_node_id)
self.instance = reference_node.add_child(** self.cleaned_data)
self.instance = reference_node.add_child(**cl_data)
self.instance.move(reference_node, pos=position_type)
else:
self.instance = self.Meta.model.add_root(** self.cleaned_data)
self.instance = self.Meta.model.add_root(**cl_data)
else:
# this is needed in django >= 1.2
self.instance.save()
Expand Down

0 comments on commit 688c2f4

Please sign in to comment.