Thanks for the tips. I have edited my model's layout as you recommended.
It seems I was going about it from the wrong starting point. By making the query you suggested (starting at AgencyDoc instead of Documents) I was able to get the information I wanted. You've increased my knowledge a fair bit today, and for that I am appreciative. Kind regards, Robert On Oct 4, 12:06 am, "Russell Keith-Magee" <[EMAIL PROTECTED]> wrote: > On Sat, Oct 4, 2008 at 5:34 AM, Robert > > <[EMAIL PROTECTED]> wrote: > > > I've been banging my head against this problem for several days now, > > and have decided to ask for help. I've read the Django DB API and > > searched this mailing list. > > > Assuming the following models: > > >http://dpaste.com/82218/ > > As a matter of style - here are two suggestions for your agency_docs table: > 1) It should be named 'AgencyDoc'. Convention is that classes should > be capitalized, with camel case, and singular (since each instance of > the class will be an instance of an object). > 2) The foreign keys should be agency and document, not agency_id and > document_id. Under the hood, the _id suffix will be used in the > database, indicating that the column contains an ID, but from a > Python/Django API level, you will be dealing with fully fledged > objects, not IDs > > > What I would like to do is access the 'study_level' field. > > > When my view is called, I will know the Agency name, and I'd like to > > show a list of documents that correspond to that agency. > > > Generic views allow me to quite easily do this, but what I can't seem > > to do is access the information in agency_docs. When I print out each > > document, I'd like to put the 'study_level' information beside each. > > > I though that 'select_related' would be my answer, because as far as I > > can tell, I need to do a classic JOIN. > > select_related() won't really help here. select_related() is really > just a database optimization; when one object has a foreign key on a > second object, you can get the data to both objects using a single SQL > call. When performance is critical, this can be a helpful > optimization. However, it doesn't provide any additional access to > data that wouldn't already be available. > > > Any help would be greatly appreciated. > > To solve this problem, you need to look very closely at the data you > are trying to display. Are you looking to display a list of agencies? > Or are you trying to display a list of the relationships that agencies > have? The fact that you want to show study_level suggests that what > you should be building is a generic relation on AgencyDoc, not Agency. > > You can select AgencyDoc objects that point to a specific agency > (AgencyDoc.objects.filter(agency_id__name='foo')), and then each > object in the list will have access to the study level for the > relationship. Each AgencyDoc object has an agency and a document, so > you have full access to all the data in the connected objects. > > Yours, > Russ Magee %-) --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---

