gpt4 book ai didi

python - 为什么 Django 应该尝试在字段查找中自动调用参数?

转载 作者:太空宇宙 更新时间:2023-11-04 09:02:05 24 4
gpt4 key购买 nike

>>> d = {'AUD': 1, 'GBP': 2}
>>> Currency.objects.filter(currency_code__in=d)
[<Currency: Australia Dollar (AUD)>, <Currency: United Kingdom Pound (GBP)>]
>>> Currency.objects.filter(currency_code__in=d.keys())
[<Currency: Australia Dollar (AUD)>, <Currency: United Kingdom Pound (GBP)>]

好的是有道理的。但是:

>>> Currency.objects.filter(currency_code__in=d.keys)
[<Currency: Australia Dollar (AUD)>, <Currency: United Kingdom Pound (GBP)>]

WTF?为什么 Django 试图调用我发送的参数?什么功能需要?我真的期望不同,因为当然:

>>> d.keys
<function keys>
>>> 'AUD' in d.keys
# TypeError: argument of type 'function' is not iterable

最佳答案

这是一个未记录且未经测试的功能,已在 1.7 中弃用,请参阅相关弃用建议:Deprecate callable QuerySet filter arguments .

仅供引用,source code正在做的部分。

根据Django deprecation process , 它会 fire up a DeprecationWarning在 1.7 和 1.8 中,最终将在 1.9 中删除:

Callable arguments for querysets were an undocumented feature that was unreliable. It's been deprecated and will be removed in Django 1.9.

Callable arguments were evaluated when a queryset was constructed rather than when it was evaluated, thus this feature didn't offer any benefit compared to evaluating arguments before passing them to queryset and created confusion that the arguments may have been evaluated at query time.

关于python - 为什么 Django 应该尝试在字段查找中自动调用参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24210436/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com