Django

Code

Changeset 316

Show
Ignore:
Timestamp:
07/26/05 11:11:43 (3 years ago)
Author:
adrian
Message:

Fixed #163 -- Added 'pk' database API option, which is a shorthand for (primary_key)exact

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/contrib/comments/models/comments.py

    r292 r316  
    7979        from django.core.exceptions import ObjectDoesNotExist 
    8080        try: 
    81             return self.get_content_type().get_object_for_this_type(id__exact=self.object_id) 
     81            return self.get_content_type().get_object_for_this_type(pk=self.object_id) 
    8282        except ObjectDoesNotExist: 
    8383            return None 
     
    194194        from django.core.exceptions import ObjectDoesNotExist 
    195195        try: 
    196             return self.get_content_type().get_object_for_this_type(id__exact=self.object_id) 
     196            return self.get_content_type().get_object_for_this_type(pk=self.object_id) 
    197197        except ObjectDoesNotExist: 
    198198            return None 
  • django/trunk/django/contrib/comments/templatetags/comments.py

    r292 r316  
    8181            # because do_comment_form() validates hard-coded object IDs. 
    8282            try: 
    83                 self.content_type.get_object_for_this_type(id__exact=self.obj_id) 
     83                self.content_type.get_object_for_this_type(pk=self.obj_id) 
    8484            except ObjectDoesNotExist: 
    8585                context['display_form'] = False 
     
    204204            obj_id = tokens[3] 
    205205            try: # ensure the object ID is valid 
    206                 content_type.get_object_for_this_type(id__exact=obj_id) 
     206                content_type.get_object_for_this_type(pk=obj_id) 
    207207            except ObjectDoesNotExist: 
    208208                raise template.TemplateSyntaxError, "'%s' tag refers to %s object with ID %s, which doesn't exist" % (self.tag_name, content_type.name, obj_id) 
     
    284284            obj_id = tokens[3] 
    285285            try: # ensure the object ID is valid 
    286                 content_type.get_object_for_this_type(id__exact=obj_id) 
     286                content_type.get_object_for_this_type(pk=obj_id) 
    287287            except ObjectDoesNotExist: 
    288288                raise template.TemplateSyntaxError, "'%s' tag refers to %s object with ID %s, which doesn't exist" % (self.tag_name, content_type.name, obj_id) 
     
    339339            obj_id = tokens[3] 
    340340            try: # ensure the object ID is valid 
    341                 content_type.get_object_for_this_type(id__exact=obj_id) 
     341                content_type.get_object_for_this_type(pk=obj_id) 
    342342            except ObjectDoesNotExist: 
    343343                raise template.TemplateSyntaxError, "'%s' tag refers to %s object with ID %s, which doesn't exist" % (self.tag_name, content_type.name, obj_id) 
  • django/trunk/django/contrib/comments/views/comments.py

    r57 r316  
    198198    content_type_id, object_id = target.split(':') # target is something like '52:5157' 
    199199    try: 
    200         obj = contenttypes.get_object(id__exact=content_type_id).get_object_for_this_type(id__exact=object_id) 
     200        obj = contenttypes.get_object(pk=content_type_id).get_object_for_this_type(pk=object_id) 
    201201    except ObjectDoesNotExist: 
    202202        raise Http404, "The comment form had an invalid 'target' parameter -- the object ID was invalid" 
     
    285285        raise Http404, "Somebody tampered with the comment form (security violation)" 
    286286    content_type_id, object_id = target.split(':') # target is something like '52:5157' 
    287     content_type = contenttypes.get_object(id__exact=content_type_id) 
     287    content_type = contenttypes.get_object(pk=content_type_id) 
    288288    try: 
    289         obj = content_type.get_object_for_this_type(id__exact=object_id) 
     289        obj = content_type.get_object_for_this_type(pk=object_id) 
    290290    except ObjectDoesNotExist: 
    291291        raise Http404, "The comment form had an invalid 'target' parameter -- the object ID was invalid" 
     
    337337        content_type_id, object_id = request.GET['c'].split(':') 
    338338        try: 
    339             content_type = contenttypes.get_object(id__exact=content_type_id) 
    340             obj = content_type.get_object_for_this_type(id__exact=object_id) 
     339            content_type = contenttypes.get_object(pk=content_type_id) 
     340            obj = content_type.get_object_for_this_type(pk=object_id) 
    341341        except ObjectDoesNotExist: 
    342342            pass 
  • django/trunk/django/contrib/comments/views/karma.py

    r57 r316  
    2020        raise Http404, "Anonymous users cannot vote" 
    2121    try: 
    22         comment = comments.get_object(id__exact=comment_id) 
     22        comment = comments.get_object(pk=comment_id) 
    2323    except comments.CommentDoesNotExist: 
    2424        raise Http404, "Invalid comment ID" 
     
    2727    karma.vote(request.user.id, comment_id, rating) 
    2828    # Reload comment to ensure we have up to date karma count 
    29     comment = comments.get_object(id__exact=comment_id) 
     29    comment = comments.get_object(pk=comment_id) 
    3030    t = template_loader.get_template('comments/karma_vote_accepted') 
    3131    c = Context(request, { 
  • django/trunk/django/contrib/comments/views/userflags.py

    r57 r316  
    1717    """ 
    1818    try: 
    19         comment = comments.get_object(id__exact=comment_id, site_id__exact=SITE_ID) 
     19        comment = comments.get_object(pk=comment_id, site_id__exact=SITE_ID) 
    2020    except comments.CommentDoesNotExist: 
    2121        raise Http404 
     
    3232def flag_done(request, comment_id): 
    3333    try: 
    34         comment = comments.get_object(id__exact=comment_id, site_id__exact=SITE_ID) 
     34        comment = comments.get_object(pk=comment_id, site_id__exact=SITE_ID) 
    3535    except comments.CommentDoesNotExist: 
    3636        raise Http404 
     
    5151    """ 
    5252    try: 
    53         comment = comments.get_object(id__exact=comment_id, site_id__exact=SITE_ID) 
     53        comment = comments.get_object(pk=comment_id, site_id__exact=SITE_ID) 
    5454    except comments.CommentDoesNotExist: 
    5555        raise Http404 
     
    7373def delete_done(request, comment_id): 
    7474    try: 
    75         comment = comments.get_object(id__exact=comment_id, site_id__exact=SITE_ID) 
     75        comment = comments.get_object(pk=comment_id, site_id__exact=SITE_ID) 
    7676    except comments.CommentDoesNotExist: 
    7777        raise Http404 
  • django/trunk/django/core/meta.py

    r311 r316  
    11471147            continue 
    11481148        lookup_list = kwarg.split(LOOKUP_SEPARATOR) 
     1149        # pk="value" is shorthand for (primary key)__exact="value" 
     1150        if lookup_list[-1] == 'pk': 
     1151            lookup_list = lookup_list[:-1] + [opts.pk.name, 'exact'] 
    11491152        if len(lookup_list) == 1: 
    11501153            _throw_bad_kwarg_error(kwarg) 
  • django/trunk/django/models/auth.py

    r292 r316  
    223223        from django.models.auth import users 
    224224        from django.conf.settings import REGISTRATION_COOKIE_DOMAIN 
    225         user = users.get_object(id__exact=user_id) 
     225        user = users.get_object(pk=user_id) 
    226226        user.last_login = datetime.datetime.now() 
    227227        user.save() 
     
    275275    def get_edited_object(self): 
    276276        "Returns the edited object represented by this log entry" 
    277         return self.get_content_type().get_object_for_this_type(id__exact=self.object_id) 
     277        return self.get_content_type().get_object_for_this_type(pk=self.object_id) 
    278278 
    279279    def get_admin_url(self): 
  • django/trunk/django/models/core.py

    r292 r316  
    1515        "Returns the current site, according to the SITE_ID constant." 
    1616        from django.conf.settings import SITE_ID 
    17         return get_object(id__exact=SITE_ID) 
     17        return get_object(pk=SITE_ID) 
    1818 
    1919class Package(meta.Model): 
  • django/trunk/django/views/admin/doc.py

    r182 r316  
    1 import os 
    2 import re 
    3 import inspect 
    41from django.core import meta 
    52from django import templatetags 
     
    1512except ImportError: 
    1613    doc = None 
     14import inspect, os, re 
    1715 
    1816# Exclude methods starting with these strings from documentation 
     
    129127                'title'  : title, 
    130128                'site_id': settings_mod.SITE_ID, 
    131                 'site'   : sites.get_object(id__exact=settings_mod.SITE_ID), 
     129                'site'   : sites.get_object(pk=settings_mod.SITE_ID), 
    132130                'url'    : simplify_regex(regex), 
    133131            }) 
  • django/trunk/django/views/defaults.py

    r233 r316  
    88    from django.models.core import contenttypes 
    99    try: 
    10         content_type = contenttypes.get_object(id__exact=content_type_id) 
    11         obj = content_type.get_object_for_this_type(id__exact=object_id) 
     10        content_type = contenttypes.get_object(pk=content_type_id) 
     11        obj = content_type.get_object_for_this_type(pk=object_id) 
    1212    except ObjectDoesNotExist: 
    1313        raise Http404, "Content type %s object %s doesn't exist" % (content_type_id, object_id) 
  • django/trunk/docs/db-api.txt

    r299 r316  
    9898...retrieves all polls published in January 2005 that have a question starting with "Would." 
    9999 
     100For convenience, there's a ``pk`` lookup type, which translates into 
     101``(primary_key)__exact``. In the polls example, these two statements are 
     102equivalent:: 
     103 
     104    polls.get_object(id__exact=3) 
     105    polls.get_object(pk=3) 
     106 
     107``pk`` lookups also work across joins. In the polls example, these two 
     108statements are equivalent:: 
     109 
     110    choices.get_list(poll__id__exact=3) 
     111    choices.get_list(poll__pk=3) 
     112 
    100113Ordering 
    101114======== 
  • django/trunk/docs/overview.txt

    r265 r316  
    9494    django.models.news.ReporterDoesNotExist: Reporter does not exist for {'id__exact': 2} 
    9595 
     96    # Lookup by a primary key is the most common case, so Django provides a 
     97    # shortcut for primary-key exact lookups. 
     98    # The following is identical to reporters.get_object(id__exact=1). 
     99    >>> reporters.get_object(pk=1) 
     100    John Smith 
     101 
    96102    # Create an article. 
    97103    >>> from datetime import datetime 
     
    201207        # Use the Django API to find an object matching the URL criteria. 
    202208        try: 
    203             a = articles.get_object(pub_date__year=year, pub_date__month=month, id__exact=article_id) 
     209            a = articles.get_object(pub_date__year=year, pub_date__month=month, pk=article_id) 
    204210        except articles.ArticleDoesNotExist: 
    205211            raise Http404 
  • django/trunk/docs/tutorial01.txt

    r301 r316  
    5050module-level variables representing Django settings. Edit the file and change 
    5151these settings to match your database's connection parameters: 
    52      
    53     * ``DATABASE_ENGINE`` -- Either 'postgresql', 'mysql' or 'sqlite3'.  
     52 
     53    * ``DATABASE_ENGINE`` -- Either 'postgresql', 'mysql' or 'sqlite3'. 
    5454      More coming soon. 
    55     * ``DATABASE_NAME`` -- The name of your database, or the full path to  
     55    * ``DATABASE_NAME`` -- The name of your database, or the full path to 
    5656      the database file if using sqlite. 
    5757    * ``DATABASE_USER`` -- Your database username (not used for sqlite). 
     
    135135 
    136136.. admonition:: Philosophy 
    137     
     137 
    138138   A model is the single, definitive source of data about your 
    139139   data. It contains the essential fields and behaviors of the data you're 
     
    244244      (polls) with a plural version of the object name (polls and choices). (You 
    245245      can override this behavior.) 
    246        
     246 
    247247    * Primary keys (IDs) are added automatically. (You can override this, too.) 
    248      
     248 
    249249    * The foreign key relationship is made explicit by a ``REFERENCES`` statement. 
    250      
     250 
    251251    * It's tailored to the database you're using, so database-specific field types 
    252252      such as ``auto_increment`` (MySQL), ``serial`` (PostgreSQL), or ``integer 
     
    257257If you're interested, also run the following commands: 
    258258 
    259     * ``django-admin.py sqlinitialdata polls`` -- Outputs the initial-data  
     259    * ``django-admin.py sqlinitialdata polls`` -- Outputs the initial-data 
    260260      inserts required for Django's admin framework. 
    261      
    262     * ``django-admin.py sqlclear polls`` -- Outputs the necessary ``DROP  
     261 
     262    * ``django-admin.py sqlclear polls`` -- Outputs the necessary ``DROP 
    263263      TABLE`` statements for this app, according to which tables already exist 
    264264      in your database (if any). 
    265        
     265 
    266266    * ``django-admin.py sqlindexes polls`` -- Outputs the ``CREATE INDEX`` 
    267267      statements for this app. 
    268      
     268 
    269269    * ``django-admin.py sqlall polls`` -- A combination of 'sql' and 
    270270      'sqlinitialdata'. 
     
    373373    [What's up] 
    374374 
     375    # Lookup by a primary key is the most common case, so Django provides a 
     376    # shortcut for primary-key exact lookups. 
     377    # The following is identical to polls.get_object(id__exact=1). 
     378    >>> polls.get_object(pk=1) 
     379    What's up 
     380 
    375381    # Make sure our custom method worked. 
    376     >>> p = polls.get_object(id__exact=1) 
     382    >>> p = polls.get_object(pk=1) 
    377383    >>> p.was_published_today() 
    378384    False 
     
    380386    # Give the Poll a couple of Choices. Each one of these method calls does an 
    381387    # INSERT statement behind the scenes and returns the new Choice object. 
    382     >>> p = polls.get_object(id__exact=1) 
     388    >>> p = polls.get_object(pk=1) 
    383389    >>> p.add_choice(choice='Not much', votes=0) 
    384390    Not much 
  • django/trunk/docs/tutorial03.txt

    r299 r316  
    243243    def detail(request, poll_id): 
    244244        try: 
    245             p = polls.get_object(id__exact=poll_id) 
     245            p = polls.get_object(pk=poll_id) 
    246246        except polls.PollDoesNotExist: 
    247247            raise Http404