如何在django中过滤以便将每个对象排序为正确的父对象?

时间:2016-04-01 00:47:47

标签: python django sorting filter

我正在构建一个特定于位置的应用。我想做的是按城市和州显示学校名单。页面按州组织。反过来,州页面显示按城市排序的学校。

国家 ---城市 ------学校 ---城市 ------学校

我可以通过状态页面显示城市列表。但是,我得到的是所有城市的列表,而不是将它们分类到正确的状态,而不是整理出来自不同州的城市。我也无法将学校列表过滤到正确的城市。城市和学校出现在每个州 - 甚至是不正确的州。

models.py

class SchoolList(models.Model):
    school_list_image = models.ForeignKey(Photo, default='')
    school_list_state = models.ForeignKey('place.state', default='')
    school_list_city = models.ForeignKey('place.city', default='')
    school_list_zip_code = models.ForeignKey('place.zip', default='')
    school_list_address = models.ForeignKey('place.address', default='')
    school_list_contact = models.ForeignKey(Contact)
    school_list_university = models.ForeignKey('place.university', default='')
    school_list_professionalschool = models.ForeignKey('place.professionalschool', default='')
    school_list_summary = models.ForeignKey(Summary, default='')    

    def __str__(self):
        return self.school_list_university.university_name

class State(models.Model):
    state_name = models.CharField(max_length=20, default='')
    state_abbreviation = models.CharField(max_length=2, default='')

    class Meta:
        ordering = ['-state_name']

    def __str__(self):
        return self.state_name

class City(models.Model):
    city_name = models.CharField(max_length=55, default='')

    class Meta:
        ordering = ['-city_name']
        verbose_name_plural = 'Cities'

    def __str__(self):
        return self.city_name

class Zip(models.Model):
    zipcode = models.CharField(max_length=15, default='')

    class Meta:
        ordering = ['-zipcode']

    def __str__(self):
        return self.zipcode

class University(models.Model):
    university_name = models.CharField(max_length=55, default='')
    university_summary = models.CharField(max_length=255, default='')
    university_image = models.ForeignKey(Photo, default='')

    class Meta:
        verbose_name_plural = 'Universities'
        ordering = ['-university_name']

    def __str__(self):
        return self.university_name

class ProfessionalSchool(models.Model):
    school_name = models.CharField(max_length=100, default='')

    class Meta:
        verbose_name_plural = 'Professional Schools'
        ordering = ['-school_name']

    def __str__(self):
        return self.school_name

class Address(models.Model):
    address = models.CharField(max_length=100, default='')
    address2 = models.CharField(max_length=100, default='', blank=True)
    address3 = models.CharField(max_length=100, default='', blank=True)

    class Meta:
        verbose_name_plural = 'Addresses'

    def __str__(self):
        return self.address

views.py

class StateDetail(ListView):
    model = StateSchoolListArticle
    template = 'state_detail.html'

    context_object_name = 'article_state_list'

    def get_context_data(self, **kwargs):
        context = super(StateDetail, self).get_context_data(**kwargs)
        context['school_list'] = SchoolList.objects.all().order_by('school_list_city')
        return context

urls.py

url(r'^(?P<slug>[-\w]+)/$', StateDetail.as_view(), name='state_detail'),

template.html

{% for school in school_list %}
<h2>{{ school.school_list_city.city_name }}</h2>
<div class="school_image">
    {% cloudinary school.school_list_image.image format="jpg" crop="fill" %}
</div>
<div class="demo_wrapper">
    <div class="row">
        <div class="medium-4 columns">
            <div class="school_data_wrapper">
                <h3>{{ school.school_list_university.university_name }}</h3>
                <h4 style="margin-bottom: 10px;">
                    {{ school.school_list_professionalschool.school_name }}      </h4>
                <h4>{{ school.school_list_address.address }}</h4>
                <h4>{{ school.school_list_city.city_name }}, {{ school.school_list_state.state_name }} {{ school.school_list_zipcode.zipcode }}</h4>                                                
                <h4><a href="tel:555555555">{{ school.school_list_contact.telephone }}</a>
               </h4>
                <h4><a href="" rel="external nofollow">
                    {{ school.school_list_contact.website }}</a>
                </h4>
            </div>
        </div>
        <div class="medium-8 columns">
            <h3>Summary</h3>
            <p style="padding: 20px 0;">{{ school.school_list_summary.summary }}<a href="link to university detail page">...more</a></p>
        </div>
    </div>
</div>

1 个答案:

答案 0 :(得分:2)

这不是一个真正的答案,但是将它放在评论中的时间太长了。

我根本不喜欢SchoolList模型。它违反了Normal Forms,它迟早会咬你。模型应代表现实世界。所以当你有一个邮政编码时,它就不存在了。它与一个城市有关。当你有城市时,它与一个国家有关。所以模型看起来应该是这样的:

class State(models.Model):
    state_name = models.CharField(max_length=20, default='')
    state_abbreviation = models.CharField(max_length=2, default='')

class City(models.Model):
    city_name = models.CharField(max_length=55, default='')
    state = models.ForeignKey(State)

class Zip(models.Model):
    zipcode = models.CharField(max_length=15, default='')
    city = models.ForeignKey(City)

class Address(models.Model):
    zip = models.ForeignKey(Zip)
    ...

现在,当你有地址时,你希望在那里有一个ForeignKey(Zip)。通过这种方式,您可以在其中传递定义ZipCityState,这样您就可以(或者更像是应该)从{{1}中删除它们模型。 然后,当你想按州过滤学校时,你可以这样做:

SchoolList