that works (to my surprise). thanks a lot.

Am 04.10.2006 um 18:49 schrieb DavidA:

>
>
> va:patrick.kranzlmueller wrote:
>> I have a model with blog-postings, assigned to a specific user - the
>> user is stored in the table "posting" via foreignkey.
>>
>> the user is part of a grade, part of a school and part of a state -
>> all of which have overview-pages.
>> so, to get all the postings for a state I have to do the following:
>>
>> 1. search for schools in that state
>> 2. search for grades in that school
>> 3. search for users within that grade
>> 4. and finally, get all the postings for that user
>>
>> seems a little complicated.
>
> Not sure if I understand your models exactly, but you don't need to do
> four "searches" (queries), you just need to do one query with the
> constraint that spans the four relations. Assuming your models look
> something like this:
>
> class State(models.Model):
>     code = models.CharField(maxlength=2)
>
> class School(models.Model):
>     state = models.ForeignKey(State)
>
> class Grade(models.Model):
>     school = models.ForeignKey(School)
>
> class User(models.Model):
>     grade = models.ForeignKey(Grade)
>
> class Posting(models.Model):
>     user = models.ForeignKey(User)
>
> Then you can just do this to find all postings for users in grades in
> schools in New York:
>
>    Posting.objects.filter(user__grade__school__state__code='NY')
>
> Which will translate into one query looking something like this:
>
>   select * from posting
>   join user on posting.user_id = user.id
>   join grade on user.grade_id = grade.id
>   join school on grade.school_id = school.id
>   join state on school.state_id = state.id
>   where state.code = 'NY'
>
> (abbreviated - the actual Django version of the SQL will be quite a  
> bit
> more verbose due to fully qualified column names).
>
> Of course, I may have completely misunderstood your models and this  
> may
> be completely off base!
> -Dave
>
>
> >


--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to