I want to filter and get only those data related to its related objects data if only related to its parent object child objects have data. For eg: I have the following models:
class Collection(models.Model):
date_of_collection=models.DateField()
class Product(models.Model):
name=models.CharField(max_length=100)
collection = models.ForeignKey(Collection)
class Price(models.Model):
price = models.FloatField()
products = models.ForeignKey(Products, on_delete=models.CASCADE)
and i have data related to models as:
Collection:
+----+--------------------+
| id | date_of_collection |
+----+--------------------+
| 1 | 2019-01-17 |
| 2 | 2019-01-30 |
| 3 | 2019-02-01 |
| 4 | 2019-02-02 |
+----+--------------------+
Products:
+----+--------------------------------+
| id | name | collection |
+----+--------------------------------+
| 1 | product 1 | 3 |
| 2 | product 2 | 1 |
| 3 | product 3 | 1 |
| 4 | product 4 | 4 |
+----+--------------------------------+
Price:
| id | price | product |
+--------+------------------+-----------------------+
| 1 | 10.00 | 1 |
| 2 | 20.00 | 1 |
| 3 | 12.00 | 3 |
+--------+------------------+-----------------------+
here I have price related to only 1
and 3
product so I only want those products based on queryset that i only want to filter based on specific date_of_collection.
I have tried the following queryset:
collection_month = Collection.objects.filter(date_of_collection__month=2)
product = Product.objects.filter(collection_id__in=collection_month).exclude(price_set__price=None)
is it the way I do or some next way.. it gives sometimes bad result. How do I do it.