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

时间:2021-08-31 21:54:22

I am trying to run a queryset with the value of the filter being the values inside a dictionary.

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

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

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

second attempt

第二次尝试

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"

How can I accomplish this? Should I just take the extra step of creating a new list and appending the dictionary values into the list? And then run the queryset on the list? I'm not sure what to do.

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

EDIT:

编辑:

when I print nearestzips I get:

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

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

BUT, when I print nearestzips.values(), I get:

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

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

1 个解决方案

#1


1  

Looks like the nearestzips is a subclass of the QuerySet which is lost some compatibility. Try to convert values_list() to the simple python list:

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

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

#1


1  

Looks like the nearestzips is a subclass of the QuerySet which is lost some compatibility. Try to convert values_list() to the simple python list:

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

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