google-code-export / django-mptt

Automatically exported from code.google.com/p/django-mptt
Other
0 stars 0 forks source link

Working with inherited models #64

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Am trying to get mptt working with this model and no luck so far.  Not sure if 
this is because 
mptt doesn't understand inheritance or because i am doing something wrong.

Here is the model:

class Stuff(models.Model):
    name = models.CharField(max_length=140)
    parent = models.ForeignKey('self', null=True, blank=True, related_name='children')

mptt.register(Stuff)

class ToDo(Stuff):
    what_type = models.CharField(max_length=1, choices=WHAT_TYPE, default='t')
    status = models.CharField(_('status'), max_length=1, choices=STATUS_CHOICES, default=1)

class Concept(Stuff):
    priority = models.CharField(_('status'), max_length=1, choices=PRIORITY_CHOICES, 
default='C')

And the code where I am saving (tried many different options!)

        obj = ToDo(**field_vals)
        obj.tags = " ".join(set(self.keyw_used))
        obj.save()

        # add parent
        if self.parent:
            obj.move_to(self.parent)

        obj.save()

Am getting the error  'Options' object has no attribute 'parent_attr' when 
trying to add to ToDo.
Error occurring here:
def is_root_node(self):
return getattr(self, '%s_id' % self._meta.parent_attr) is None 

although self does have mptt attributes:
>>> self.__dict__
{'rght': None, 'lft': None, 'stuff_ptr_id': 1L,  'parent_id': None, 'tree_id': 
None, .....  }

Original issue reported on code.google.com by phoebebr...@gmail.com on 30 Sep 2009 at 9:11

GoogleCodeExporter commented 9 years ago
The model inherits fields from the superclass, but the _meta doesn't inherit 
the fields. You need to call register() on every model, not just on the parent 
model.

This is avoided in the abstract-model-refactor branch, where register() has 
been removed and everything's done via inheritance from MPTTModel (see Issue 34 
)

Original comment by craig.ds@gmail.com on 20 Sep 2010 at 10:38