class Tyre(models.Model):
name = models.CharField(max_length=128)
description = models.CharField(max_length=256)
product_type = models.CharField(max_length=128)
class TyrePrices(models.Model):
tyre = models.ForeignKey(Tyre, on_delete=models.CASCADE)
price = models.IntegerField()
discount = models.IntegerField()
description = models.TextField(max_length=256)
discount_price = models.IntegerField()
stock = models.BooleanField(default=True)
This is model, and the requirement is want to filter the tyre according to tyre price range(which is at the second model).
How can I do this?
Tyre.objects.filter(tyreprices__price__gte=x,tyreprices__price__lte=y)
If you want a range. Otherwise drop one or the other. Or adjust for exclusions.
When you have a relation, Django create automaticaly a reverse relation ( doc here :
tyre = models.ForeignKey(Tyre, on_delete=models.CASCADE)
can be noted this way :
tyre = models.ForeignKey(Tyre, on_delete=models.CASCADE, related_name="tyreprices")
For your query just do :
Tyre.objects.filter(tyreprices__price__range=(x,y))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.