Я пытался переписать фильтры, написанные на 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
в теме.