Kaydet (Commit) e2409750 authored tarafından Malcolm Tredinnick's avatar Malcolm Tredinnick

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
üst 1de4bf0b
...@@ -83,6 +83,11 @@ class Model(object): ...@@ -83,6 +83,11 @@ class Model(object):
def _get_pk_val(self): def _get_pk_val(self):
return getattr(self, self._meta.pk.attname) 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): def __repr__(self):
return smart_str(u'<%s: %s>' % (self.__class__.__name__, unicode(self))) return smart_str(u'<%s: %s>' % (self.__class__.__name__, unicode(self)))
......
...@@ -1284,6 +1284,17 @@ won't add the automatic ``id`` column. ...@@ -1284,6 +1284,17 @@ won't add the automatic ``id`` column.
Each model requires exactly one field to have ``primary_key=True``. 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 Admin options
============= =============
......
...@@ -33,6 +33,11 @@ __test__ = {'API_TESTS': """ ...@@ -33,6 +33,11 @@ __test__ = {'API_TESTS': """
>>> a.id >>> a.id
1L 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. # Access database columns via Python attributes.
>>> a.headline >>> a.headline
'Area man programs in Python' 'Area man programs in Python'
......
...@@ -56,6 +56,15 @@ DoesNotExist: Employee matching query does not exist. ...@@ -56,6 +56,15 @@ DoesNotExist: Employee matching query does not exist.
>>> Employee.objects.filter(pk__in=['ABC123','XYZ456']) >>> Employee.objects.filter(pk__in=['ABC123','XYZ456'])
[<Employee: Fran Bones>, <Employee: Dan Jones>] [<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 got married and changed her last name.
>>> fran = Employee.objects.get(pk='XYZ456') >>> fran = Employee.objects.get(pk='XYZ456')
>>> fran.last_name = 'Jones' >>> fran.last_name = 'Jones'
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment