In Django filter statement what's the differen

2019-01-24 01:33发布

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

.filter(name__exact='Alex')

and

.filter(name='Alex')

Thanks

2条回答
ら.Afraid
2楼-- · 2019-01-24 01:56

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.
查看更多
ゆ 、 Hurt°
3楼-- · 2019-01-24 02:02

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.

查看更多
登录 后发表回答