Monday, October 10, 2011

Need advice on ForeignKey query problem.

I've got these two tables defined where a Facility can have multiple
schedules but a schedule can have only one facility.

class Facility(models.Model):
id = models.CharField(max_length=2, primary_key=True)
name = models.CharField(max_length=30)
....


class Schedule(models.Model):
facility = models.ForeignKey(Facility)
...

I would like a query that returns one record for each Facility and that
record would contain at least the id (if not more) of ALL linked
schedules. So far I've only been able to return a queryset that
contains a record for each schedule at each facility. My last attempt
looked like this...

records = models.Facility.objects.select_related().filter(id='62')

is there a pythonic way to do this? I only need reasonable efficiency,
there's not that much data and the query is probably only run 200 times
in an 8 hour shift.

--
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 django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate