Display dropdown Choice field in Django admin

1k Views Asked by At

I am begin to Django and I start create a simple personal blog. I want to create model Category that related to parrent Category. I have read about display Foreign Key's choices in django-admin. It only work with some static choices. But I cannot query data from database for choices. This is how I am trying to do:

class Category (models.Model):

    name = models.CharField(max_length=255)
    slug = models.CharField(unique=True, blank=True, max_length=255)
    parent_category = models.CharField(max_length=255, blank=True, choice = CATEGORIES_CHOICE)

I want CATEGORIES_CHOICE = Category.object.all(). Any one have an idea to resolve this problem?

1

There are 1 best solutions below

2
On BEST ANSWER

You can use a django self-referential foreign key !

parent_category = models.ForeignKey("self",blank=True, null=True)

or

parent_category = models.ForeignKey("Category",blank=True, null=True)

it's exactly the same