Thanks.   A related question - how would I tell the template to
display only one address, say the address which
has address.address_type='P'

I can do it the laborious way which is to get all, loop, and then use
{% ifequal %} to display the one I want.  But then I have to pass a
variable containing 'P'.   Is there a better way either in the
template or in the select statement?

Thanks

On Apr 24, 6:20 pm, Alex Gaynor <alex.gay...@gmail.com> wrote:
> On Fri, Apr 24, 2009 at 7:18 PM, adrian <adrian...@gmail.com> wrote:
>
> > Given these models from the doc:
>
> > class Publication(models.Model):
> >    title = models.CharField(max_length=30)
>
> > class Article(models.Model):
> >    headline = models.CharField(max_length=100)
> >    publications = models.ManyToManyField(Publication)
>
> > What do I need in the view and template to print an article and a list
> > of all (or just one of) the publications it is in?
>
> > I expected to be able to do something like:
>
> > article = Article.objects.select_related().get(id=object_id)
>
> > return render_to_response('detail.html', {
> >                    'article': article ,
> >                }
>
> > and then in the template:
>
> > Headline is: {{ article.headline }}<br>
> > List of publications is appears in:
> > {% for publication in article.publications %}
> > {{ publication.title }}
> >  {% endfor %}
>
> > But this always appears blank even when there is data in the fields.
> > I think I have to do something special for the intermediate table but
> > I don't see in the doc what that is?
>
> > Thanks
>
> You need to use article.publications.all since a Manager(which is what
> article.publications is) isn't iterable, just like you can't iterate over
> Author.objects, you need to use all.
>
> Alex
>
> --
> "I disapprove of what you say, but I will defend to the death your right to
> say it." --Voltaire
> "The people's good is the highest law."--Cicero
--~--~---------~--~----~------------~-------~--~----~
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+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to