On Tue, 2008-03-25 at 14:26 -0400, Vladimir Sanjinez wrote:
> Hi, I am new in this django, but I have already tried a few examples
> I found on the web, but I have some doubts and queries:
> 
> Given the following classes:
> 
> Book class (models.Model):
>  author =models.ForeignKey(Author)
>  publisher = models.ForeignKey(Editor)
>  Publication = models.DateTimeField ()
>  Pages = models.IntegerField()
> 
> Author class (models.Model):
>  Name = models.CharField (...)
>  Nationality = models.CharField (...)
> 
> Editor class (models.Model):
>  Name = models.CharField (...)
>  Country = models.CharField (...)
> 
> 
> As could perform the following query:
> 
> All books were published between 2005-2007, whose author
> Be "Juan Perez" and belongs to a publisher whose country is "Spain"

A very nice thing about Django's querysets is that you can build them up
one piece at a time. So, books that were published between 2005 and
2007. Suppose d1 and d2 are datetime objects representing Jan 1, 2005
and Dec 31, 2007, respectively. You can write:

        Book.objects.filter(Publication__range=(d1,d2))
        
Now, add the author restriction:

        
Book.objects.filter(Publication__range=(d1,d2)).filter(author__Name='Juan 
Perez')
        
And finally, the publisher's country:

        
Book.objects.filter(Publication__range=(d1,d2)).filter(author__Name='Juan 
Perez').filter(publisher__Country='Spain')
        
The only potentially hard bit here is the year query. Since all these
filters are against different fields, you could write them as a single
filter:

        Book.objects.filter(Publication__range=(d1,d2),
                            author__Name='Juan Perez',
                            publisher__Country='Spain')

Regards,
Malcolm

-- 
Save the whales. Collect the whole set. 
http://www.pointy-stick.com/blog/


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to