On Sep 16, 2:30 am, "Lance F. Squire" <[EMAIL PROTECTED]> wrote: > On Sep 15, 5:21 pm, Daniel Roseman <[EMAIL PROTECTED]> > wrote: > > > Probably the easiest solution, if you know you're > > always going to be requesting a certain lookup, is to define a custom > > method on the model: > > > class System(models.Model): > > ... field declarations ... > > > def get_header_pic(self): > > return > > self.system_pictures.filter(image_category__name='Header_Pic') > > > and then in the template you can just do {{ info.get_header_pic }} > > As there can be 20 or more images associated with a system, This is > more what I was looking for. Thanks!!! > > However, I can't seem to get this to work. > > Here are the new lines in System: > > def get_header_pic(self): > return > self.system_pictures.filter(image_category__name='Header_Pic') > > def get_header_logo(self): > return > self.system_pictures.filter(image_category__name='Header_Logo') > > I put them just above the Class Admin: line. > > Here is the new lines in the template: > > <IMG src="{{ info.get_header_pic.location }}" > width="{{ info.get_header_pic.width }}" > height="{{ info.get_header_pic.height }}" border="0" > name="{{ info.get_header_pic.name }}"> > > <IMG src="{{ info.get_header_logo.location }}" > width="{{ info.get_header_logo.width }}" > height="{{ info.get_header_logo.height }}" border="0" > name="{{ info.get_header_logo.name }}"> > > Lance
Sorry, my mistake. filter() always gives you a queryset - because there might be more than one instance of the related model that fits the criteria - so you can't jut reference the location/width directly. The best way is to do something like this: try: return self.system_pictures.filter(image_category__name='Header_Pic')[0] except IndexError: pass which will always get the first related image in that category, and silently swallows the error that's thrown if there's no such picture. By the way, I would recommend playing around in the shell (./manage.py shell) with this - you'll get a much better feel for how it all fits together. - DR. --~--~---------~--~----~------------~-------~--~----~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---