In a Django project, I have a hierarchical model using MPTT defined like this in models.py:
class Structure(MPTTModel):
name = models.CharField(max_length=200, unique=True)
parent = TreeForeignKey('self', null=True, blank=True, related_name='children')
[...]
I'm using FeinCMS to show this hierarchical data in admin pages. I do it like this in admin.py:
class StructureAdmin(tree_editor.TreeEditor):
search_fields = ('name',)
[...]
admin.site.register(Structure, StructureAdmin)
In the admin model page, it works perfectly and the hierarchy can be seen:
It also works when editing or adding:
I have another model in models.py:
class Track(models.Model):
initialStructure = models.ForeignKey(Structure , related_name='track_initialStructure')
finalStructure = models.ForeignKey(Structure, related_name='track_finalStructure')
[...]
However, when adding a new element of this kind, the hierarchy can not be seen:
I've tried to use tree_editor.TreeEditor for the admin view of Track but it gives a lot of errors because Track is not hierarchical, but some of its ForeignKey's are. How could I show the hierarchy when editing an element of model Track?
Thank you very much.
Try changing:
finalStructure = models.ForeignKey(Structure, related_name='track_finalStructure')
to:
finalStructure = TreeForeignKey(Structure, related_name='track_finalStructure')
of course, after importing TreeForeignKey
from django-mptt:
from mptt.fields import TreeForeignKey
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With