Display Hierarchical Categories in Django Admin

153 views
Skip to first unread message

Rahul Chauhan

unread,
Aug 11, 2021, 1:57:33 PM8/11/21
to Django users
Hi All,

I have a category Model like this one which is based on Adjacency List.

class Category(models.Model):
    name = models.CharField(blank=False, max_length=200)
    slug = models.SlugField(null=False)
    parent = models.ForeignKey('self', blank=True, null=True, related_name='child',on_delete=models.SET_NULL)

I want to display the model has hierarchical/nested structure in the Django admin. There are modules available such as TreeBeard or Django-MPTT but I dont want to use them.

Tried everything, but unable to achieve the above.

Any help in this direction will be highly appreciated.

Thanks.

This e-mail is intended only for the named person or entity to which it is addressed and contains valuable business information that is privileged, confidential and/or otherwise protected from disclosure. If you received this e-mail in error, any review, use, dissemination, distribution or copying of this e-mail is strictly prohibited. Please notify us immediately of the error via e-mail to discl...@email-abuse.com and please delete the e-mail from your system, retaining no copies in any media. We appreciate your cooperation.

Sebastian Jung

unread,
Aug 12, 2021, 5:05:18 AM8/12/21
to django...@googlegroups.com
Hello Rahul,

i Django MPTT tutorial you find in modely.py:

from django.db import models
from mptt.models import MPTTModel, TreeForeignKey

class Genre(MPTTModel):
    name = models.CharField(max_length=50, unique=True)
    parent = TreeForeignKey('self', on_delete=models.CASCADE, null=True, blank=True, related_name='children')

    class MPTTMeta:
        order_insertion_by = ['name']

This works. 

Regards

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/25736f0b-19e3-4ad2-a774-552774425894n%40googlegroups.com.

Rahul Chauhan

unread,
Aug 12, 2021, 5:33:01 AM8/12/21
to Django users

Thanks for the suggestion.
But I don't want to use any third party addon/module such as MPTT or Tree-Beard.

Regards
R
Reply all
Reply to author
Forward
0 new messages