Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.

#1 March 25, 2008 19:26:43

Vladimir S.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

doubts about query (fixed)


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"



Atte


Vladi

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

Offline

#2 March 25, 2008 19:58:37

Malcolm T.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

doubts about query (fixed)


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 django-users@googlegroups.com
To unsubscribe from this group, send email to
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en-~----------~----~----~----~------~----~------~--~---

Offline

Board footer

Moderator control

Enjoy the 16th of December
PoweredBy

The Forums are managed by develissimo stuff members, if you find any issues or misplaced content please help us to fix it. Thank you! Tell us via Contact Options
Leave a Message
Welcome to Develissimo Live Support