将子子分类为子分类为父 django
Sort subchild into child into parent django
这主要是工作(感谢对上一个问题的大量帮助。
现在我想将子子对象分类到子对象中,然后将子对象分类到它们的父对象中。具体来说,我想将学校按城市分类到各自的州。然后,我希望学校以正确的状态显示到正确的城市。我要找的是这样的:
状态
--- 城市
-----学校
-----学校
-----学校
--- 城市
-----学校
-----学校
-----学校
--- 城市
-----学校
-----学校
-----学校
现在,我将城市分类到州内。但是,我想不出让学校进入城市。我正在阅读文档中的过滤器。我认为答案就在那里。但是,当我尝试将学校过滤到城市中时,没有任何对象返回。
有没有办法检索 'school_list' 作为 'city_list' 的子级,同时仍然保持 'city_list 为状态的子级?我很想学习如何做。
编辑:添加了学校模型。在城市中按名称对学校进行排序是最理想的。
我们将不胜感激。
编辑:dkarchmer 的解决方案运行良好。再次感谢。希望这对将来的其他人有用。
models.py
class State(models.Model):
state_name = models.CharField(max_length=20, default='')
state_slug = models.SlugField()
state_image = models.ForeignKey(Image, null=True)
state_summary = models.TextField(null=True)
def __str__(self):
return self.state_slug
class City(models.Model):
city_name = models.CharField(max_length=55, default='')
city_slug = models.SlugField()
state_image = models.ForeignKey(Image, null=True)
school_image = models.ForeignKey(Image, null=True, related_name='+')
state = models.ForeignKey(State, null=True)
def __str__(self):
return self.city_slug
def def sorted_schools(self):
return self.school_set.all().order_by('school_name')
class School(models.Model):
school_name = models.CharField(max_length=55, default='')
school_slug = models.SlugField()
city = models.ForeignKey(City, null=True)
def __str__(self):
return self.school_slug
views.py
class CityInStateView(ListView):
model = City
template = 'template.html'
slug_field = 'state_slug'
context_object_name = 'city_in_state_list'
def get_context_data(self, **kwargs):
context = super(CityInStateView, self).get_context_data(**kwargs)
context['state'] = self.object
context['city_list'] = self.object.city_set.all().order_by('city_name')
return context
urls.py
urlpatterns = [
url(r'^$', SchoolIndexView.as_view(), name='school_index'),
url(r'^(?P<state_slug>[\w-]+)/$', CityInStateView.as_view(), name='state_index'),
]
template.html
{% block main_content %}
<div class="row body">
<div class="main_content">
<div class="row">
<h1>{{ state.state_name }}</h1>
{% for city in city_list %}
<h2>{{ city.city_name }}</h2>
{% for school in city.sorted_schools %}
<h3>{{ school.school_name }} </h3>
{% endfor %}
{% endfor %}
</div>
</div>
</div>
{% endblock %}
这让我烦恼了一个星期。请像我五岁一样向我解释。感谢您提前提供的所有帮助。
最简单的方法是将城市模型添加到 return 学校:
class City(models.Model):
...
def sorted_schools(self):
return self.school_set.all().order_by('school_name')
然后直接从模板中使用:
<div class="row body">
<div class="main_content">
<div class="row">
<h1>{{ state.state_name }}</h1>
{% for city in city_list %}
<h2>{{ city.city_name }}</h2>
{% for school in city.sorted_schools %}
<h3>{{ school.school_name }} </h3>
{% endfor
{% endfor %}
</div>
</div>
</div>
如果您不想将该方法添加到模型中,第二种选择是使用模板标签来基本上完成相同的操作。
这主要是工作(感谢对上一个问题的大量帮助。
现在我想将子子对象分类到子对象中,然后将子对象分类到它们的父对象中。具体来说,我想将学校按城市分类到各自的州。然后,我希望学校以正确的状态显示到正确的城市。我要找的是这样的:
状态
--- 城市
-----学校
-----学校
-----学校
--- 城市
-----学校
-----学校
-----学校
--- 城市
-----学校
-----学校
-----学校
现在,我将城市分类到州内。但是,我想不出让学校进入城市。我正在阅读文档中的过滤器。我认为答案就在那里。但是,当我尝试将学校过滤到城市中时,没有任何对象返回。
有没有办法检索 'school_list' 作为 'city_list' 的子级,同时仍然保持 'city_list 为状态的子级?我很想学习如何做。
编辑:添加了学校模型。在城市中按名称对学校进行排序是最理想的。
我们将不胜感激。
编辑:dkarchmer 的解决方案运行良好。再次感谢。希望这对将来的其他人有用。
models.py
class State(models.Model):
state_name = models.CharField(max_length=20, default='')
state_slug = models.SlugField()
state_image = models.ForeignKey(Image, null=True)
state_summary = models.TextField(null=True)
def __str__(self):
return self.state_slug
class City(models.Model):
city_name = models.CharField(max_length=55, default='')
city_slug = models.SlugField()
state_image = models.ForeignKey(Image, null=True)
school_image = models.ForeignKey(Image, null=True, related_name='+')
state = models.ForeignKey(State, null=True)
def __str__(self):
return self.city_slug
def def sorted_schools(self):
return self.school_set.all().order_by('school_name')
class School(models.Model):
school_name = models.CharField(max_length=55, default='')
school_slug = models.SlugField()
city = models.ForeignKey(City, null=True)
def __str__(self):
return self.school_slug
views.py
class CityInStateView(ListView):
model = City
template = 'template.html'
slug_field = 'state_slug'
context_object_name = 'city_in_state_list'
def get_context_data(self, **kwargs):
context = super(CityInStateView, self).get_context_data(**kwargs)
context['state'] = self.object
context['city_list'] = self.object.city_set.all().order_by('city_name')
return context
urls.py
urlpatterns = [
url(r'^$', SchoolIndexView.as_view(), name='school_index'),
url(r'^(?P<state_slug>[\w-]+)/$', CityInStateView.as_view(), name='state_index'),
]
template.html
{% block main_content %}
<div class="row body">
<div class="main_content">
<div class="row">
<h1>{{ state.state_name }}</h1>
{% for city in city_list %}
<h2>{{ city.city_name }}</h2>
{% for school in city.sorted_schools %}
<h3>{{ school.school_name }} </h3>
{% endfor %}
{% endfor %}
</div>
</div>
</div>
{% endblock %}
这让我烦恼了一个星期。请像我五岁一样向我解释。感谢您提前提供的所有帮助。
最简单的方法是将城市模型添加到 return 学校:
class City(models.Model):
...
def sorted_schools(self):
return self.school_set.all().order_by('school_name')
然后直接从模板中使用:
<div class="row body">
<div class="main_content">
<div class="row">
<h1>{{ state.state_name }}</h1>
{% for city in city_list %}
<h2>{{ city.city_name }}</h2>
{% for school in city.sorted_schools %}
<h3>{{ school.school_name }} </h3>
{% endfor
{% endfor %}
</div>
</div>
</div>
如果您不想将该方法添加到模型中,第二种选择是使用模板标签来基本上完成相同的操作。