Display dropdown Choice field in Django admin

Display dropdown Choice field in Django admin,django,django-models,choicefield,Django,Django Models,Choicefield,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.CharFie

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)

#1

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


#2

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


#3

It worked but when the first time I create a category. The field parent_category cannot null. So I cannot save it.

#4

Use blank=True, null=True if you want the foreign key to be optional.

#5

It worked but when the first time I create a category. The field parent_category cannot null. So I cannot save it.

#6

Use blank=True, null=True if you want the foreign key to be optional.