Fixed #3703 -- Added pk property to models. Thanks, Collin Grady and jeromie@gmail.com.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@6346 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Malcolm Tredinnick 2007-09-16 01:57:25 +00:00
parent 1de4bf0b66
commit e2409750f8
4 changed files with 30 additions and 0 deletions

View File

@ -83,6 +83,11 @@ class Model(object):
def _get_pk_val(self):
return getattr(self, self._meta.pk.attname)
def _set_pk_val(self, value):
return setattr(self, self._meta.pk.attname, value)
pk = property(_get_pk_val, _set_pk_val)
def __repr__(self):
return smart_str(u'<%s: %s>' % (self.__class__.__name__, unicode(self)))

View File

@ -1284,6 +1284,17 @@ won't add the automatic ``id`` column.
Each model requires exactly one field to have ``primary_key=True``.
The ``pk`` property
-------------------
**New in Django development version**
Regardless of whether you define a primary key field yourself, or let Django
supply one for you, each model will have a property called ``pk``. It behaves
like a normal attribute on the model, but is actually an alias for whichever
attribute is the primary key field for the model. You can read and set this
value, just as you would for any other attribute, and it will update the
correct field in the model.
Admin options
=============

View File

@ -33,6 +33,11 @@ __test__ = {'API_TESTS': """
>>> a.id
1L
# Models have a pk property that is an alias for the primary key attribute (by
# default, the 'id' attribute).
>>> a.pk
1L
# Access database columns via Python attributes.
>>> a.headline
'Area man programs in Python'

View File

@ -56,6 +56,15 @@ DoesNotExist: Employee matching query does not exist.
>>> Employee.objects.filter(pk__in=['ABC123','XYZ456'])
[<Employee: Fran Bones>, <Employee: Dan Jones>]
# The primary key can be accessed via the pk property on the model.
>>> e = Employee.objects.get(pk='ABC123')
>>> e.pk
u'ABC123'
# Or we can use the real attribute name for the primary key:
>>> e.employee_code
u'ABC123'
# Fran got married and changed her last name.
>>> fran = Employee.objects.get(pk='XYZ456')
>>> fran.last_name = 'Jones'