Answer the question
In order to leave comments, you need to log in
How to compare dependent model fields in Django?
Hello. I have the following models in my project: Item with different fields and Prices dependent on Item, as well as price and discount_price fields. I would like to know how can I get a set of Item elements with the condition that price!=discont_price.
model.py
class Item(models.Model):
name = models.TextField(max_length=512,db_index=True)
weight = models.IntegerField(default=0)
description = models.TextField()
composition = models.TextField(default='Состав')
class Prices(models.Model):
item = models.ForeignKey(Item, on_delete=models.CASCADE)
old_price= models.DecimalField(default=100.00,max_digits=9, decimal_places=2, verbose_name='Цена')
price= models.DecimalField(default=100.00,max_digits=9, decimal_places=2, verbose_name='Цена по скидке')
def sales_view(request):
item_list = Item.objects.filter(Prices__old_price__gt=F('Prices__price'))
return render(request, 'main-page.html',
{'item_list ': item_list })
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question