N
N
Nick V2016-04-26 18:10:14
Django
Nick V, 2016-04-26 18:10:14

How to display tree structure in Django admin panel?

Good afternoon everyone. There are three models:

models.py

class RegionsModel(models.Model):
    name = models.CharField(max_length=64,
                            unique=True,  )
    def __unicode__(self):
        return self.name


class DetachmentsModel(models.Model):
    region = models.ForeignKey('RegionsModel',
                            on_delete=models.CASCADE,
                            related_name='region_children', )
    name = models.CharField(max_length=64,
                            unique=True,
                            db_index=True)

    def __unicode__(self):
        return self.name


class DepartmentsModel(MPTTModel):
    detachment = models.ForeignKey('DetachmentsModel',
                            on_delete=models.CASCADE,
                            related_name='detachment_children', )
    name = models.CharField(max_length=64,
                            unique=True,
                            db_index=True, )
    slug = models.SlugField(max_length=64,
                            unique=True,
                            primary_key=True, )

    def __unicode__(self):
        return self.name


Please explain how to make the following hierarchy using django-mptt and django-mptt-admin :
Region
|__Detachment
| |__Department
| |__Department
|__Detachment
Region
|__Detachment
| |__Department
| |__Department
|__Detachment
|__Department

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question