Reputation: 135
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?
Upvotes: 0
Views: 1013
Reputation: 1906
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
Upvotes: 2