Friday, November 22, 2013

Re: how to get at the manytomany fields in for

Small correction to what Daniel suggested:
    {% for spec in d.specialism_set.all %}
        {{ spec.field1 }} etc
    {% endfor %}

On Friday, November 22, 2013 4:23:42 PM UTC+5:30, MikeKJ wrote:
Got this model
class AdviceLevel(models.Model):
    advice = models.ForeignKey(Advice)
    organisation = models.ForeignKey(Organisation)
    specialism = models.ManyToManyField(Specialism, null=True, blank=True)
    service_restriction = models.TextField(null=True, blank=True)

in the view
    dir_list = AdviceLevel.objects.all().filter(advice=advice)

so I get a list

in the template
    {% for d in dir_list %}
        {{ d.organisation.title }}
        {{ d.servicerestriction }}    //etc
 
// but when it comes to the many to many how do I access each
// d.specialsm gets however many selected <django.db.models.fields.related.ManyRelatedManager object at 0x973bb6c>
// tried d.specialism_set.all but I get AttributeError: 'ManyRelatedManager' object has no attribute 'set' also
// tried for x in d.specialism but get  'AdviceLevel' object has no attribute 'specialsm'

So how do I access the individual fields of the manytomany results of specialism in the template please?

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/26d8a2c0-fa1f-4f48-a21e-04308ada525c%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate