Changeset 3826
- Timestamp:
- 09/25/06 08:49:01 (2 years ago)
- Files:
-
- django/trunk/django/db/models/query.py (modified) (2 diffs)
- django/trunk/docs/db-api.txt (modified) (2 diffs)
- django/trunk/tests/modeltests/basic/models.py (modified) (1 diff)
- django/trunk/tests/modeltests/custom_pk/models.py (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
django/trunk/django/db/models/query.py
r3400 r3826 713 713 # The very-last is the lookup_type (equals, like, etc). 714 714 # The second-last is the table column on which the lookup_type is 715 # to be performed. 716 # The exceptions to this are: 717 # 1) "pk", which is an implicit id__exact; 718 # if we find "pk", make the lookup_type "exact', and insert 719 # a dummy name of None, which we will replace when 720 # we know which table column to grab as the primary key. 721 # 2) If there is only one part, or the last part is not a query 722 # term, assume that the query is an __exact 715 # to be performed. If this name is 'pk', it will be substituted with 716 # the name of the primary key. 717 # If there is only one part, or the last part is not a query 718 # term, assume that the query is an __exact 723 719 lookup_type = path.pop() 724 720 if lookup_type == 'pk': … … 767 763 # Has the primary key been requested? If so, expand it out 768 764 # to be the name of the current class' primary key 769 if name is None :765 if name is None or name == 'pk': 770 766 name = current_opts.pk.name 771 767 django/trunk/docs/db-api.txt
r3774 r3826 1141 1141 1142 1142 For convenience, Django provides a ``pk`` lookup type, which stands for 1143 "primary_key". This is shorthand for "an exact lookup on the primary-key."1143 "primary_key". 1144 1144 1145 1145 In the example ``Blog`` model, the primary key is the ``id`` field, so these … … 1150 1150 Blog.objects.get(pk=14) # pk implies id__exact 1151 1151 1152 The use of ``pk`` isn't limited to ``__exact`` queries -- any query term 1153 can be combined with ``pk`` to perform a query on the primary key of a model:: 1154 1155 # Get blogs entries with id 1, 4 and 7 1156 Blog.objects.filter(pk__in=[1,4,7]) 1157 # Get all blog entries with id > 14 1158 Blog.objects.filter(pk__gt=14) 1159 1152 1160 ``pk`` lookups also work across joins. For example, these three statements are 1153 1161 equivalent:: django/trunk/tests/modeltests/basic/models.py
r3661 r3826 86 86 >>> Article.objects.get(pk=1) 87 87 <Article: Area woman programs in Python> 88 89 # pk can be used as a shortcut for the primary key name in any query 90 >>> Article.objects.filter(pk__in=[1]) 91 [<Article: Area woman programs in Python>] 88 92 89 93 # Model instances of the same type and same ID are considered equal. django/trunk/tests/modeltests/custom_pk/models.py
r3661 r3826 52 52 <Employee: Dan Jones> 53 53 54 # pk can be used as a substitute for the primary key. 55 >>> Employee.objects.filter(pk__in=['ABC123','XYZ456']) 56 [<Employee: Fran Bones>, <Employee: Dan Jones>] 57 54 58 # Fran got married and changed her last name. 55 59 >>> fran = Employee.objects.get(pk='XYZ456')
