how to use filter in django
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.
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
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.