0
What you are looking is a reverse relationship, to do that you use models.ForeignKey(‘self’, on_delete=models.CASCADE).
The official doc is here https://docs.djangoproject.com/en/1.10/ref/models/fields/#foreignkey
I hope it helps
Best
1
remove Subcategory
model and write Category
model like this
class Category(models.Model):
categoryName=models.CharField(max_length=100)
parent = models.ForeignKey("Category", null=True, blank=True)
def __unicode__(self):
return self.categoryName
and remove subcategory
field from Product
.
Writing like this is more neat
- Change the css class of an element in a Django form that uses ModelForm
- Django Custom Admin Panel Site Settings
- NoReverseMatch in 'a href' HTML link
- How do I create a many to many relationship with an existing record?
- Django dynamics ChoiceFields choices from APIs raises ValueError
Source:stackexchange.com