In Django filter statement what's the difference between __exact and equal sign (=)?

自闭症网瘾萝莉.ら 提交于 2019-11-27 17:16:57

问题


In Django filter statement what's the difference if I write:

.filter(name__exact='Alex')

and

.filter(name='Alex')

Thanks


回答1:


There is no difference, the second one implies using the __exact.

From the documentation:

For example, the following two statements are equivalent:
>>> Blog.objects.get(id__exact=14)  # Explicit form
>>> Blog.objects.get(id=14)         
# __exact is implied This is for convenience, because exact 
# lookups are the common case.



回答2:


You can look at the SQL that Django will execute by converting the queryset's query property to a string:

>>> from django.contrib.auth.models import User
>>> str(User.objects.filter(username = 'name').query)
'SELECT ... WHERE `auth_user`.`username` = name '
>>> str(User.objects.filter(username__exact = 'name').query)
'SELECT ... WHERE `auth_user`.`username` = name '

So __exact makes no difference here.



来源:https://stackoverflow.com/questions/9963200/in-django-filter-statement-whats-the-difference-between-exact-and-equal-sign

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!