为什么我的django查询集不起作用?

时间:2015-02-13 01:27:16

标签: django django-queryset

我正在尝试运行一个查询集,其中过滤器的值是字典中的值。

latest_entries = Entry.objects.filter(zipcode__in=nearestzips.values())

print latest_entries
>>>TypeError: Cannot use a multi-field GeoValuesQuerySet as a filter value.

第二次尝试

latest_entries = Entry.objects.filter(zipcode__in=nearestzips.values_list('code', flat=True))

print latest_entries
>>>ProgrammingError: invalid reference to FROM-clause entry for table "cities_postalcode" HINT perhaps you meant to reference the table alias"u0"

我怎样才能做到这一点?我是否应该采取额外的步骤来创建新列表并将字典值附加到列表中?然后在列表上运行查询集?我不知道该怎么做。

编辑:

当我打印最近的时候,我得到:

[<PostalCode:97201>,<PostalCode:97202>]

但是,当我打印nearestzips.values()时,我得到:

[distance:0, code: 97201, name: Portland, subregion: Multnomah] etc.

1 个答案:

答案 0 :(得分:1)

看起来nearestzipsQuerySet的子类,它失去了一些兼容性。尝试将values_list()转换为简单的python列表:

zip_codes = list(nearestzips.values_list('code', flat=True))
latest_entries = Entry.objects.filter(zipcode__in=zip_codes)
相关问题