Anupam
Anupam

Reputation: 15620

add custom field to each object in ListView

I am trying to add a custom field to a ListView to describe an entity more verbose-ly (so that I can then use it in the template) but surprisingly cant find a straight forward way to do that. How can I add context to each object in the list? self.object_list returns the whole list and it feels counter-intuitive to iterate through it to add this extra field.

Here's a simplified version of the code:

class AreaWiseSchoolsView(ListView):

    template_name = 'search/area.html'
    paginate_by = 15

    def get_queryset(self):
        qs = School.objects.filter(area__name=self.kwargs['areaname'])
        return qs 

    def get_context_data(self, **kwargs):
        school_type_description = ""
        context = super(AreaWiseSchoolsView, self).get_context_data(**kwargs)
        # need code here to add the custom field to each object in the list
        # school = self.something 
        # if school.area.filter(pk=9).exists(): 
        #    school_type_description = "Some description for Area 9"
        # elif school.school_type == 'ND':
        #    school_type_description = "Some description for ND"
        # elif school.school_type == 'MA':
        #     org_type_description = "Some description for MA"
        context['school_type_description'] = school_type_description
        return context

In the template, I need to able to do the following:

{% for school in object_list %}
    {{school.school_type_description}}
{% endfor %}

Also, is there a simpler way to do the above instead of overriding get_context_data()?

Upvotes: 1

Views: 2242

Answers (2)

user8060120
user8060120

Reputation:

can you add property to your school model:

class School(models.Model):
    # YOU DESCRIPTION HERE

    @property
    def school_type_description(self):
        if self.area.filter(pk=9).exists():
            return "Some description for Area 9"
        elif self.school_type == 'ND':
            return "Some description for ND"
        elif self.school_type == 'MA':
            return "Some description for MA"
       return ''

Upvotes: 1

wencakisa
wencakisa

Reputation: 5958

You can add a @property in your School model:

from django.db import models


class School(models.Model):
    # ...

    @property
    def type_description(self):
        school_type_description = 'Some default description'

        if self.area.filter(pk=9).exists(): 
            school_type_description = "Some description for Area 9"
        elif self.school_type == 'ND':
            school_type_description = "Some description for ND"
        elif self.school_type == 'MA':
            school_type_description = "Some description for MA"

        return school_type_description

And then you can directly access this property in your template:

{% for school in object_list %}
    {{ school.type_description }}
{% endfor %}

There is no need of implementing get_context_data() in your ListView now.

Upvotes: 4

Related Questions