无法将关键字 'model' 解析为字段。 Django 过滤器
Cannot resolve keyword 'model' into field. Django filters
我尝试重写过滤器,写在 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
。
通过查看 docs,您可以看到 to_name_field
将被映射到模型中的 Django 字段,因此您会收到 Django 无法 "resolve field location_district
" 因为你的模型中没有 location_district
。
尽管从未使用过 DjangoFilters,但我相信如果你真的需要命名该字段,你可以将其指向 location
。这意味着您的过滤器会像这样:
district = django_filters.ModelMultipleChoiceFilter(
name="location",
queryset=District.objects.all(),
)
或者您可以试试这个,但请注意,我不知道它是否有效
district = django_filters.ModelMultipleChoiceFilter(
name="location__district",
queryset=District.objects.all(),
)
把ModelMultipleChoiceFilter
name
改成field_name
,对我来说很合适。
我尝试重写过滤器,写在 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
。
通过查看 docs,您可以看到 to_name_field
将被映射到模型中的 Django 字段,因此您会收到 Django 无法 "resolve field location_district
" 因为你的模型中没有 location_district
。
尽管从未使用过 DjangoFilters,但我相信如果你真的需要命名该字段,你可以将其指向 location
。这意味着您的过滤器会像这样:
district = django_filters.ModelMultipleChoiceFilter(
name="location",
queryset=District.objects.all(),
)
或者您可以试试这个,但请注意,我不知道它是否有效
district = django_filters.ModelMultipleChoiceFilter(
name="location__district",
queryset=District.objects.all(),
)
把ModelMultipleChoiceFilter
name
改成field_name
,对我来说很合适。