On 07/03/07, hotani <[EMAIL PROTECTED]> wrote: > > This is the table (model) setup: > [county] (list of counties) > [office] (list of offices) > [active counties] (a relational table with a county_id, office_id and > 'active' column) > [division] (columns: 'name', 'county_id' which points to the first > county table) > > Obviously the division table is directly linked to the county table > which is just a list of names. What I need to do is extract the > divisions for a particular office, based on the active counties table. > The problem is, 'active counties' is not directly linked to divisions! > > This is how I would do it in sql, assuming the current office has an > id of '3': > > SELECT d."name" > FROM division d > LEFT JOIN active_counties c ON (d.county_id=c.county_id) > WHERE c.office_id='3' > > I'm hoping to avoid a custom SQL query from django, but right now I'm > not seeing any alternatives. >
Hmm, it depends on how exactly you've created your models. It's nothing complicated that you're doing so far and it can be expressed easily in the Django way. Assuming you have the following models: Country Office ActiveOffices country = models.ForeignKey(Country) office = models.ForeignKey(Office) active - (do you really need that? if there's no entry in a M2M relationship, it means it's not active. if it doesnt serve a specific purpose, simply use M2M instead) Division name = models.TextField() country = models.ForeignKey(Country) You could get it, the proper way, using Division.obejcts.filter(country__activeoffices__active = True, country__activeoffices_office=OfficeID). --~--~---------~--~----~------------~-------~--~----~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---