1

class Status(models.Model): someid = models.IntegerField() value = models.IntegerField() status_msg = models.CharField(max_length = 2000)

so my database look like:
 20     1234567890   'some mdg'
 20     4597434534   'some msg2'
 20     3453945934   'sdfgsdf'
 10     4503485344   'ddfgg'

so I have to fetch values contain a giving someid between some values. so let say

  val1 = '1234567890'
    val2  = '4414544544'

so my final result should be list containing of 2 entry for id = 20 how to implement this.

i tried using

list = Status.objects.filter(someid = 20, value < val2, value > val1) 

which is wrong? how to fix this.

thanks.

1
  • sorry for posting this. I have fix the problem. list = Status.objects.filter(someid=20, value__lt=val2).filter(value__gt=val1) Commented May 18, 2009 at 6:16

2 Answers 2

10

Django Query API doesn't use traditional comparison operators. It uses (field)__(operatorname)=(value) style syntax.

Your query would be:

list = Status.objects.filter(someid=20, value__lt=val2, value__gt=val1)

See Django Docs on Making Queries

Sign up to request clarification or add additional context in comments.

Comments

3

You can also use the *__range lookup:

list = Status.objects.filter(someid=20, value__range=(val1+1, val2-1))

Be aware, range lookups are 'inclusive', so you have to adapt the range boundaries. If applicable like above, this should result in the very same list as posted by Imran. Range lookups also work with dates.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.