django select_related in template

前端 未结 1 988
醉梦人生
醉梦人生 2021-01-11 21:43

Sometimes it makes sense to use select_related in a django template. For example, say I have a class extending DetailView

class DemoCarView(DetailView):
             


        
相关标签:
1条回答
  • 2021-01-11 22:11

    How about a simple method on the Car model?

    class Car(models.Model):
        ...
        def parts_with_manufacturers(self):
            return self.part_set.select_related('manufacturer')
    

    and then

    {% for part in car.parts_with_manufacturers %}
        <li>{{ part.name }} - {{ part.manufacturer.name }} </li>
    {% endfor %}
    
    0 讨论(0)
提交回复
热议问题