views:

21

answers:

1

I need to make special request to database through django.

For example:

class Model(models.Model):
     name=models.CharField()
     date=models.DateTimeField()
     other=models.TextField()

How do I ask for row which name containe word 'Hello' (it shoul ignor register of first letter) end it is must be in diapason of date, for example between 2005.08.09 and 2005.08.11?

+1  A: 

Try the following:

start_date = datetime.date(2005, 8, 9)
end_date = datetime.date(2005, 8, 11)
Model.objects.filter(name__icontains="hello").filter(date__range(start_date,end_date))

You can stack as many filters as you like and it will be built into a single SQL Query (or whatever database system you use)

Josiah
Can i pass this request in method get() if i know that there will be only one result?
Pol
Yes, with Q objects, but the performance will not be any better.
Josiah