Django

Code

Changeset 3826

Show
Ignore:
Timestamp:
09/25/06 08:49:01 (2 years ago)
Author:
russellm
Message:

Made pk a generic expansion for the primary key, rather than just an expansion for idexact.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/db/models/query.py

    r3400 r3826  
    713713            # The very-last is the lookup_type (equals, like, etc). 
    714714            # 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 
    723719            lookup_type = path.pop() 
    724720            if lookup_type == 'pk': 
     
    767763    # Has the primary key been requested? If so, expand it out 
    768764    # to be the name of the current class' primary key 
    769     if name is None
     765    if name is None or name == 'pk'
    770766        name = current_opts.pk.name 
    771767 
  • django/trunk/docs/db-api.txt

    r3774 r3826  
    11411141 
    11421142For 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".  
    11441144 
    11451145In the example ``Blog`` model, the primary key is the ``id`` field, so these 
     
    11501150    Blog.objects.get(pk=14) # pk implies id__exact 
    11511151 
     1152The use of ``pk`` isn't limited to ``__exact`` queries -- any query term  
     1153can 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     
    11521160``pk`` lookups also work across joins. For example, these three statements are 
    11531161equivalent:: 
  • django/trunk/tests/modeltests/basic/models.py

    r3661 r3826  
    8686>>> Article.objects.get(pk=1) 
    8787<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>] 
    8892 
    8993# Model instances of the same type and same ID are considered equal. 
  • django/trunk/tests/modeltests/custom_pk/models.py

    r3661 r3826  
    5252<Employee: Dan Jones> 
    5353 
     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 
    5458# Fran got married and changed her last name. 
    5559>>> fran = Employee.objects.get(pk='XYZ456')