How To Configure Parent/Child relationship in the Admin where the parent and child models are the same - Foreign key back to self

I have this model :

class Categories(models.Model):
    parent = models.ForeignKey('self', on_delete=models.CASCADE, null=True, blank=True)
    category_name = models.CharField(max_length=100, unique=True)
    credit_debit = models.CharField(choices=[('C','Credit'),('D','Debit')], max_length=1, default='C')

And you will notice that this model allows a category heirarchy - by being self referencing.

How do I sanely show this in Admin - my thought was for the Parent to have a normal Admin model, and for the children to be inlined underneath. I have other models in admin where I use inlines to display related data, but i can’t for the life of me work out how to configure admin in this case.
Any help appreciated.

This might work for you?

from django.contrib import admin

class CategoryInline(admin.TabularInline):
  model = Categories

@admin.register(Categories)
class CategoryAdmin(admin.ModelAdmin):
  list_display = ("id", "category_name", "parent")
  inlines = [CategoryInline]

  def get_queryset(self, request):
    """Now maybe you want to only show the "root" categories, the ones that have no parent, then you can override the `queryset` attribute or the `get_queryset` method"""
    return super().get_queryset(request).filter(parent__isnull=True)

Does this solves your problem?

I get an ‘Already Registered’ exception.

Even trying to introduce a proxy model for Categories that I can inline results in an exception.

Maybe you can share a bit of the code that is present on your admin.py for the respective app that the Categories model lives in.
That will help diagnose the possible root cause of the error.

Sorry - False alarm :frowning: - I double checked in order to post the code, and I hadn’t removed the initial definition - it now works.

Thank you

1 Like