On 5/2/2009 6:07 AM, Paddy Joy wrote:
> I have a model "Car" with a related model "Car_Attribute" where I
> store instance specific details.
> 
> 
> class Car(models.Model):
>     name = models.CharField(max_length=50)
> 
> class Car_Attribute(models.Model):
>     car = models.ForeignKey(Car)
>     key = models.CharField(max_length=50)
>     data = models.TextField()
> 
> I have written a custom template filter to retrieve attributes
> 
> from django import template
> 
> register = template.Library()
> 
> @register.filter(name='car_tag_get')
> def car_tag_get(value, arg):
>     try:
>         return value.car_attribute_set.get(key=arg).data
>     except:
>         return "#CAR_ATTRIBUTE_NOT_FOUND key: %s" % arg
> car_tag_get.is_safe = False
> 
> and in my templates I use
> 
> {{ car|car_tag_get:"colour" }}
> 
> This works fine but is there an easier or more practical way?

<http://docs.python.org/reference/datamodel.html#object.__getattr__> on 
your Car model is perhaps the way to go.

-- 
George

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

Reply via email to