将自定义字段添加到 ListView 中的每个对象
add custom field to each object in ListView
我正在尝试向 ListView 添加自定义字段以更详细地描述实体(以便我可以在模板中使用它)但令人惊讶的是找不到直接的方法来做到这一点。如何向列表中的 each 对象添加上下文?
self.object_list
returns 整个列表,迭代它以添加这个额外字段感觉违反直觉。
这是代码的简化版本:
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
在模板中,我需要能够执行以下操作:
{% for school in object_list %}
{{school.school_type_description}}
{% endfor %}
此外,是否有更简单的方法来执行上述操作而不是覆盖 get_context_data()?
您可以在您的 School
模型中添加一个 @property
:
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
然后您可以在您的模板中直接访问此 属性:
{% for school in object_list %}
{{ school.type_description }}
{% endfor %}
现在不需要在您的 ListView
中实施 get_context_data()
。
你可以将 属性 添加到你的学校模型中吗:
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 ''
我正在尝试向 ListView 添加自定义字段以更详细地描述实体(以便我可以在模板中使用它)但令人惊讶的是找不到直接的方法来做到这一点。如何向列表中的 each 对象添加上下文?
self.object_list
returns 整个列表,迭代它以添加这个额外字段感觉违反直觉。
这是代码的简化版本:
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
在模板中,我需要能够执行以下操作:
{% for school in object_list %}
{{school.school_type_description}}
{% endfor %}
此外,是否有更简单的方法来执行上述操作而不是覆盖 get_context_data()?
您可以在您的 School
模型中添加一个 @property
:
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
然后您可以在您的模板中直接访问此 属性:
{% for school in object_list %}
{{ school.type_description }}
{% endfor %}
现在不需要在您的 ListView
中实施 get_context_data()
。
你可以将 属性 添加到你的学校模型中吗:
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 ''