Sagar Kanabar
Sagar Kanabar

Reputation: 464

where condition based on another field value in django

I have table with following structure:

==========================================
itemName minPrice maxPrice curPrice
========================================== 
   A         10      20        8 
   B         15      25        12
   c         20      30        25

In mysql is like that

select * from Items where curPrice<minPrice

but i want in django which should return like "A,B item name result".

I already tried with filter but i can not get success.

Upvotes: 0

Views: 261

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 599490

To compare a value in a filter with another field on the model, use F() objects:

from django.db.models import F
Item.objects.filter(curPrice__lt=F('minprice'))

Upvotes: 1

Related Questions