【发布时间】:2019-10-08 07:31:16
【问题描述】:
我尝试重写过滤器,在 Django 1.1 到 2.1 上编写
我有一个名为Apartment 的复杂模型,其中包含一个Location 模型。 Location 包括 District 模型。
那么,我的模型代码如下:
class District(models.Model):
district_number = models.IntegerField(_('district'))
title = models.CharField(_('district name'), max_length=100)
city = models.ForeignKey(City, on_delete=models.PROTECT)
class Meta:
unique_together = ('city', 'district_number',)
def __str__(self):
return self.title
class Location(models.Model):
apartment = models.OneToOneField(Apartment, related_name='location', on_delete=models.CASCADE)
coordinate_long = models.DecimalField(max_digits=15, decimal_places=10)
coordinate_lat = models.DecimalField(max_digits=15, decimal_places=10)
zip_code = models.IntegerField(_('zip'))
district = models.ForeignKey(District, on_delete=models.PROTECT)
subway_station = models.ForeignKey(SubwayStation, on_delete=models.PROTECT)
city = models.ForeignKey(City, on_delete=models.PROTECT)
address = models.CharField(_('human readable address of apartment'), max_length=250)
def __str__(self):
return self.address
过滤器是
district = django_filters.ModelMultipleChoiceFilter(
name="location_district",
queryset=District.objects.all(),
)
在新版本中,我将name 更改为to_field_name。
当我尝试启动时,这会抛出一个错误 - Cannot resolve keyword 'district' into field. Choices are: apartment_type, apartment_type_id, bedrooms_count, co_ownership, date_added, descriptions, economy_effective, economy_effective_id, energy_effective, energy_effective_id, favorite_lists, financial_info, floor, id, is_published, location, manager, manager_id, photos, plan, price, publish_type, publish_type_id, rooms, rooms_count, services, square, title, video_url
我不太明白ModelMultipleChoiceFilter 是如何工作的,以及如何从Location 上获得嵌套模型District。
【问题讨论】:
标签: django django-filter django-2.1 django-filters