Saturday, October 3, 2009

Trying to make django-mptt work with inherited models

Have spent quite a few hours on this one but not familiar enough with
django inerds to make much progress.

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

This is because the ToDo model is not in the mptt registery.

I tried adding an attribute to register to say it was inherited so
that the __init__ method of mptt would add the ToDo and Concept models
to the registry but not all the fields. ie.

in models:
mptt.register(Stuff)
mptt.register(ToDo, inherited=True)
mptt.register(Concept, inherited=True)

and hacked __init__:

if model in registry:
raise AlreadyRegistered(
_('The model %s has already been registered.') %
model.__name__)
registry.append(model)

#added by PHB
if not inherited:

# Add tree options to the model's Options
opts = model._meta
opts.parent_attr = parent_attr
opts.right_attr = right_attr


This got me past the first error but it's still putting the mptt
fields in the _meta of the ToDo model and failing in the models
pre_save with:

AttributeError: 'ToDo' object has no attribute 'lft'


I need to find some way of telling django these fields are in the
parent class and not the child. Any suggestions very welcome as way
out of my depth there!


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com
For more options, visit this group at http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate