2012-04-01 73 views

回答

19

沒有區別,第二個意味着使用__exact。

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. 
12

你可以看一下這Django會通過查詢集的query財產轉換爲執行SQL一串:

>>> 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 ' 

所以__exact這裏沒有什麼區別。

相關問題