0

I want query a row where my variable is within the range of 2 fields in my model.

Supposed I have x = 100 Model = Product with 3 fields price_start, price_end, category.

I want to know which category is my 'x' variable.

How can i query this in django?

p.s. this is just a simplified example of my problem. Supposing price_start and price_end does not overlap, how to know the category.

2 Answers2

2

You can try like this using gte and lte:

products = Product.objects.filter(price_start__gte=x, price_end__lte=x)

for product in products:
    print(product.category)

# Or

products.values_list('category')
ruddra
  • 50,746
  • 7
  • 78
  • 101
  • so...it is not returning a query when done this way. It works when start and end of range are the same field tho like if price__gte=10,price__lte=20. – Jonathan Chiong Apr 05 '19 at 15:15
  • nope, it should work just fine :) . Please ensure to have price_start greater than x and price_end less than x. – ruddra Apr 05 '19 at 16:37
0

for ORM try this

x = yourModel.objects.filter(start_price__level__lte= "number",price_end__level__gte"number")

so x will be queryset of model

you can see in this post

Here's a link!

  • Can you tell me what does __level__ do here? its not in the example in docs. is this for older version? i use latest version of django. Sorry if noob question.. – Jonathan Chiong Apr 05 '19 at 11:21