Hierarchical data on admin pages in Django

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 use FeinCMS to display this hierarchical data on admin pages. I do it like this: admin.py:

 class StructureAdmin(tree_editor.TreeEditor): search_fields = ('name',) [...] admin.site.register(Structure, StructureAdmin) 

On the admin model page, it works fine, and you can see the hierarchy: enter image description here

It also works when editing or adding:

enter image description here

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 type, the hierarchy cannot be seen:

enter image description here

I tried to use tree_editor.TreeEditor to view admin in Track, but it gives a lot of errors because Track is not hierarchical, but some of its ForeignKey. How can I show hierarchy when editing a track model element?

Many thanks.

+8
django django-mptt feincms
source share
1 answer

Try changing:

 finalStructure = models.ForeignKey(Structure, related_name='track_finalStructure') 

in

 finalStructure = TreeForeignKey(Structure, related_name='track_finalStructure') 

of course, after importing TreeForeignKey from django-mptt:

 from mptt.fields import TreeForeignKey 
+5
source share

All Articles