m2m through intermediate model: template friendly syntax not working. dj1.3

41 views
Skip to first unread message

brycenesbitt

unread,
Jul 13, 2012, 4:46:54 PM7/13/12
to django...@googlegroups.com
I'm able to get an intermediate m2m model to show data, but not with the template-friendly "_set" syntax:

    category    = models.Category.objects.select_related().get(pk=pk)
    entries                 = category.entry.order_by('-temp_sort_order').filter(temp_sort_order__gte=0)
    for entry in entries:
        assert isinstance(entry, models.Entry)
        ce = models.CategoryEntry2.objects.get(entry=entry, category=category)
        pprint('1: ' + ce.wiki + str(entry.ce))
        foo = entry.ce_set.get(category=category) #'Entry' object has no attribute 'ce_set'
        pprint('2: ' + foo.wiki + str(foo.ce))  #'Entry' object has no attribute 'ce_set'
    for entry in category.ce_set.all:           #'Category' object has no attribute 'ce_set'
        pprint('1: ' + entry.wiki)

Any ideas?

class Category(models.Model):
    title           = models.CharField(max_length=1024,null=True,blank=True)
    entry           = models.ManyToManyField(Entry,null=True,blank=True,
                                             related_name='ce',
                                             through='CategoryEntry2',
                                             )

brycenesbitt

unread,
Jul 16, 2012, 3:03:55 AM7/16/12
to django...@googlegroups.com
Using 'for entry in category.ce_.all', as suggested elsewhere also fails   

brycenesbitt

unread,
Jul 16, 2012, 3:10:07 PM7/16/12
to django...@googlegroups.com
I'm still stuck on this.  Is there any tutorial example that shows access m2m intermediate model data from a template.  The issue is the template library won't accept parameters.  Do I have to write a custom tag to get the m2m intermediate data fields?

Ramiro Morales

unread,
Jul 16, 2012, 3:24:33 PM7/16/12
to django...@googlegroups.com
The problem you've found isn't related to access to the relationship model
from template but that you aren't using the right names to access them.

Reading
https://docs.djangoproject.com/en/1.4/ref/models/fields/#manytomanyfield
and
https://docs.djangoproject.com/en/1.4/topics/db/models/#intermediary-manytomany
will be of great help.

--
Ramiro Morales
Reply all
Reply to author
Forward
0 new messages