Friday, July 30, 2010

how to get parent of related m2m model instance from within a template?

I have a model, which has a related model, which then has a related
m2m field (See defs below).

I want to add a method to the m2m model (ie: "Button") which, when
called in a template, will get me the original record that called the
relation. Is this possible? See method attempt below in the "Button"
model, called "get_duid". The point in this attempt is to be able to
return the original id within the template when iterating through the
buttons of a particular "DocumentUnit".

class DocumentUnit(models.Model):
parent = models.ForeignKey('self', null=True, blank=True,
unittype = models.ForeignKey(DocumentUnitType,
content = models.TextField()

class DocumentUnitType(models.Model):
name = models.CharField(max_length=40)
description = models.CharField(max_length=255, blank=True)
buttons = models.ManyToManyField('Button', null=True,
blank=True, related_name='button_du_types')

class Button(models.Model):
btn_code = models.CharField(max_length=20)
title = models.CharField(max_length=50, blank=True)
href = models.CharField(max_length=150, blank=True)
viewname = models.CharField(max_length=50, blank=True)
css_class = models.CharField(max_length=70, blank=True)
text = models.CharField(max_length=50, blank=True)
prefix_id = models.CharField(max_length=20)
order = models.IntegerField(blank=True, null=True)

def get_duid(self):

Thanks for any help you can give me with this.

You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to
To unsubscribe from this group, send email to
For more options, visit this group at

No comments:

Post a Comment