Gustavo Picon is sharing code with you

Bitbucket is a code hosting site. Unlimited public and private repositories. Free for small teams.

Don't show this again

tabo / django-treebeard

Efficient tree implementations for Django 1.0+ :: https://tabo.pe/projects/django-treebeard/

Clone this repository (size: 602.0 KB): HTTPS / SSH
hg clone https://bitbucket.org/tabo/django-treebeard
hg clone ssh://hg@bitbucket.org/tabo/django-treebeard

Issues

#15 Models fail to update in 1.2 beta (trunk)

Reported by paul.bersch (last edited )

I'm using the svn trunk version of Django and treebeard 1.52. When I try to update an existing model instance in the admin, the model data does not update. However, the "Position" and "Relative To" attributes do update properly.

It looks to me like the data is getting read from the database at some point in the form processing. I'm using an overridden model.save() method, and when I output the data in the object when that overridden save method is called, the data is the values from the database rather than from the admin form.

I think I've nailed it down to line 175 in forms.py:

self.instance = self.Meta.model.objects.get(pk=self.instance.pk)

When I comment that line out, the model data saves properly but the treebeard parts break. I'm new to Django and I don't have my head wrapped around custom forms yet, so unfortunately this is where my debugging hits a brick wall.

I've tested the same code on Django 1.1.1 and it works properly.

I've attached my models.py in case that helps.

Thanks for the great project and documentation, treebeard has been very helpful to me.

Status: resolved Responsible: Gustavo Picon Type: bug Priority: major
Milestone: none Component: none Version: none

Attachments

Comments and changes

  1. #1 Gustavo Picon

    written

    • Changed status from new to open.
  2. #2 Anonymous

    written

    Hi there, look at the code in forms.py where the default form class for treebeard is define. Modify the def save() to:

        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']
            if self.instance.pk is None:
                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.move(reference_node, pos=position_type)
                else:
                    self.instance = self.Meta.model.add_root(** self.cleaned_data)
            else:
                self.instance.save() <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< <<< SOLUTION!!!
                if reference_node_id:
                    reference_node = self.Meta.model.objects.get(
                        pk=reference_node_id)
                    self.instance.move(reference_node, pos=position_type)
                else:
                    if self.is_sorted:
                        self.instance.move(self.Meta.model.get_first_root_node(),
                                                            pos='sorted-sibling')
                    else:
                        self.instance.move(self.Meta.model.get_first_root_node(),
                                           pos='first-sibling')
                # Reload the instance
            self.instance = self.Meta.model.objects.get(pk=self.instance.pk)
            super(MoveNodeForm, self).save(commit=commit)
            return self.instance
    
  3. #3 Gustavo Picon

    written

    Working on a test case to reproduce this...

    But note that the because of the fact that this _IS_ working in 1.1.X, there is the possibility that this is a bug in Django.

  4. #4 Jury Koryakov

    written

    Have the same problem with updating an existing model (Django-1.2-beta-1). Thanks for Anonymous about solution. It seems to work well.

  5. #5 Gustavo Picon

    written

    Guys, I need a test case for this. It would help a lot!

  6. #6 Gustavo Picon

    written

    • Changed status from open to resolved.

    Fixed in 755fc99e0247

    Thanks Anonymous

Add comment / attachment

Show/hide preview

Verification: Please write the text from the image in the box (letters only)

captcha

Is that you, Humanoid? Is this me?