On 1 déc, 14:20, Daniel Roseman <[EMAIL PROTECTED]> wrote:
> On Dec 1, 12:15 pm, Marco <[EMAIL PROTECTED]> wrote:
>
>
>
> > Hello,
>
> > I have this in my models.py :
>
> > class Device(models.Model):
> >         name = models.CharField(max_length=30)
>
> > class DeviceProperty(models.Model):
> >         key = models.CharField(max_length=30)
> >         value = models.CharField(max_length=80)
> >         device = models.ForeignKey(Device)
>
> > In my views.py, I'm doing :
> > deviceList = Device.objects.all() and put this list to the response
>
> > In my template, I'm currently displaying the items like this :
> > {% for device in deviceList %}
> >       {{ device.name }}
> >       ...
> > {% endfor %}
>
> > My question is : how can I also display items from my DeviceProperty
> > class ? Like it is described in the model, I have a list of
> > DeviceProperty for each Device. So inside of my "for" loop, I'd like
> > to display the associated DeviceProperty items.
>
> > Any idea ?
>
> > Thanks.
>
> Just carry on with the same logic.
>
> {% for device in deviceList %}
>       {{ device.name }}
>       {% for property in device.deviceproperty_set.all %}
>           {{ property.key }}: {{ property.value }}
>       {% endfor %}
> {% endfor %}

And eventually change your view to:

 deviceList = Device.objects.all().select_related()

(this should avoid one extra db query per property)


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

Reply via email to