我试图重写在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
的工作原理,以及如何从District
上获得嵌套模型Location
。
答案 0 :(得分:1)
通过查看docs,您可以看到string folder = Dts.Variables["User::varProcessingFolderLocation"].Value.ToString();
MessageBox.Show(folder);
Dts.Variables["User::ProductFolderList"].Value = System.IO.Directory.GetDirectories(folder);
MessageBox.Show(Dts.Variables["User::ProductFolderList"].Value.ToString())
将被映射到模型中的Django字段,因此为什么会收到Django无法“解析字段{ {1}}”,因为您的模型中没有to_name_field
。
尽管从未使用过DjangoFilters,但我相信,如果您确实需要命名该字段,则可以将其指向location_district
。这意味着您的过滤器将需要以下内容:
location_district
或者您可以尝试一下,但是请注意,我不知道它是否可以工作
location
答案 1 :(得分:1)
将ModelMultipleChoiceFilter
name
更改为field_name
,对我来说很好。