Thank you Tom and Daniel.  Starting to see some light here.

Mike

On May 28, 1:25 am, Daniel Roseman <roseman.dan...@googlemail.com>
wrote:
> On May 27, 2:59 pm, Tom Evans <tevans...@googlemail.com> wrote:
>
>
>
> > On Wed, 2009-05-27 at 06:27 -0700, adelaide_mike wrote:
> > > Very beginner here.
>
> > > My model has two classes, Suburb, the parent, and Street, the child:
>
> > > class      Suburb(models.Model):
> > >            suburb_name = models.CharField(max_length=72)
> > >            postcode = models.CharField(max_length=4)
> > >            def __unicode__(self):
> > >                    return '%s %s' % (self.suburb_name, self.postcode)
>
> > > class      Street(models.Model):
> > >            street_name = models.CharField(max_length=72)
> > >            suburb = models.ForeignKey(Suburb)
> > >            def __unicode__(self):
> > >                    return '%s, %s' % (self.street_name, 
> > > unicode(self.suburb))
>
> > > In my view I populate the variable streets:
>
> > > def search_street(request):
> > >     query = request.GET['query']
> > >     streets = Street.objects.filter(street_name__istartswith = query)
> > >     # Do a case-insensitive search on the starts-with name of the
> > > street.
> > >     return render_to_response('wha/street_select.html', {'streets':
> > > streets, 'query': query})
>
> > > The page search_select.html contains:
>
> > > <ul>
> > >     {% for street in streets %}
> > >         <li><a href=street.id>{{street.street_name}}
> > > {{suburb.suburb_name}}</a></li>
> > >     {% endfor %}
> > >     </ul>
>
> > > This displays only the street name, and not the suburb_name.  I need
> > > it to display as in the admin ie:
> > > street_name suburb_name postcode
>
> > > What change should I make or where can I see an example?
>
> > > TIA
>
> > > Mike
>
> > {{ street.suburb.suburb_name }}
>
> Note that although this does give the right answer, it'll be very
> inefficient in terms of database access, as you'll need to hit the
> database to look up the suburb on every single street.
>
> Easy fix - add select_related() to the lookup in the view:
> streets = Street.objects.filter(street_name__istartswith =
> query).select_related()
> --
> 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 
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