【问题标题】:Cannot resolve keyword 'model' into field. Django filters无法将关键字“模型”解析为字段。 Django 过滤器
【发布时间】: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


    【解决方案1】:

    ModelMultipleChoiceFiltername 更改为field_name,这对我很有效。

    【讨论】:

      【解决方案2】:

      通过查看 docs,您可以看到 to_name_field 将映射到模型中的 Django 字段,因此您会收到 Django 无法“解析字段 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(),
      )
      

      【讨论】:

      • 已经试过了。有一个错误 - 'District' 对象没有属性 'location'
      • 您是否尝试过完全删除name?此外,如果您分享您的过滤器类,那就太好了。
      • 嗯,我的选项已经不多了。也许这会有所帮助,但我假设你也经历过这个django-filter.readthedocs.io/en/master/guide/…
      • 感谢您的推荐,但我已经阅读了。我认为这需要更好地理解项目逻辑。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-18
      • 2015-10-30
      • 1970-01-01
      • 1970-01-01
      • 2013-08-29
      相关资源
      最近更新 更多