2008/10/3 Robert <[EMAIL PROTECTED]>:
>
> 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/
>
> 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.
>
> However, I've tried:
>>>> a = Agency.objects.get(name='Agency1')
>>>> d = a.documents.select_related().all()
>>>> d[0].agency_docs.study_level
> but I get the 'has no attribute 'agency_docs' error.  I believe I've
> tried every conceivable combination, I've stuck _set onto the ends of
> things, I'm completely frustrated.

select_related() is another thing, and it's useful to avoid multiple
queries when you have to access to related_fields.

You must give us the model you have set to understand.

if it's like:

class Agency:
   name = TextField

class Document:
   agency = ForeignKey(Agency, related_name="docs")
   study_level = IntegerField

you can access to agency_docs with
a = Agency.objects.select_related().get(name='Agency1')
docs[0] = a.docs
print doc.study_level

if you don't set the related_name, you must use document_set:

a = Agency.objects.select_related().get(name='Agency1')
docs = a.document_set
print docs[0].study_level



-- 
Alessandro Ronchi
Skype: aronchi
http://www.alessandroronchi.net

SOASI Soc.Coop. - www.soasi.com
Sviluppo Software e Sistemi Open Source
Sede: Via Poggiali 2/bis, 47100 Forlì (FC)
Tel.: +39 0543 798985 - Fax: +39 0543 579928

Rispetta l'ambiente: se non ti è necessario, non stampare questa mail

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

Reply via email to