I have two django model named Category and Product. In Category model has "is_featured" boolean field.
I need category list in JSON format where is_featured=true and the list of products. The models are like following.
from django.db import models
class Category(models.Model):
name=models.CharField(max_length=25, blank=False, null=False)
is_featured=models.BooleanField(default=False)
parent=models.ForeignKey('self', on_delete=models.CASCADE)
def __str__(self):
return self.name
class Product(models.Model):
name=models.CharField(max_length=25, blank=False, null=False)
category=models.ForeignKey(Category, on_delete=models.CASCADE)
uom=models.CharField(max_length=10)
price=models.FloatField()
def __str__(self):
return '%s %s %s %s' % (self.name, self.category.name, self.price, self.uom)
My expected category list (is_featured=true) result will be look like following.
{
"id":1,
"name":"Foods",
"products":[
{
"id":2,
"name":"Chicken Fri",
"price":80
},
{
"id":4,
"name":"Tomato Salad",
"price":20
}
]
},
{
"id":3,
"name":"Vegitable",
"products":[
{
"id":3,
"name":"Potato",
"price":20
},
{
"id":6,
"name":"Bean",
"price":55
}
]
}