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,
related_name='children')
unittype = models.ForeignKey(DocumentUnitType,
related_name='du')
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):
return self.button_du_types.pk


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 django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment