On May 27, 12:20 am, Daniel Roseman <roseman.dan...@googlemail.com> wrote: >
On May 26, 3:08 pm, Joshua Partogi <joshua.j...@gmail.com> wrote: > > > > >
> > Hi all, > > > I have a very trivial case that somebody might have solved
before, but I can > > not get my head around. > > > I have this two objects
that is related to each other: > > > ---- > > class Product(models.Model): >
>     name = models.CharField(max_length=50) > > > class
Picture(models.Model): > >     product = models.ForeignKey(Product) > > >
---- > > > Now I want to display this list of Products and the related
picture. I tried > > looking in the docs how to get the related picture via
template but could > > not get the workaround for it. > > > Has anyone been
in this case before and has solved it? > > > Thank you very much in advance.
> > You have a foreign key from picture to product, not the other way >
round - so there are potentially several pictures for each product. If >
this isn't what you want, perhaps you should use a OneToOneField > instead.
> > However, assuming that you just want the first picture for any >
product, you can do this: > > {% for product in products %} >     {{
product.name }} - {{ product.picture_set.all.0 }} > {% endfor %}
Hi Daniel. I never thought you can do that. :-)

Yeah we have the foreign key from picture to product because one product may
have several pictures. That's why OneToOne is not an option for us. I wish I
can define OneToMany to Picture from Product.

Thanks for the hint anyway. We're going to try that.
-- 
Join Scrum8.com.

http://scrum8.com/jpartogi/
http://twitter.com/scrum8

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