G
G
GlamorousCar2021-03-05 21:50:36
Django
GlamorousCar, 2021-03-05 21:50:36

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='Цена по скидке')

I almost got it done with
views.py
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 })

but then the resulting set contains duplicates. Most likely, it should be done in a completely different way. I would be glad to hear any comments and instructions.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
JRazor, 2021-03-05
@GlamorousCar

What's the problem then? Is this a google request https://stackoverflow.com/questions/5877306/remove... ?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question