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

前端 未结 2 1858
無奈伤痛
無奈伤痛 2020-12-16 10:38

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

.filter(name__exact=\'Alex\')

and

.filter(name=\'Alex\')
         


        
2条回答
  •  被撕碎了的回忆
    2020-12-16 11:05

    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.

提交回复
热议问题