Changeset 6346
- Timestamp:
- 09/15/07 20:57:25 (10 months ago)
- Files:
-
- django/trunk/django/db/models/base.py (modified) (1 diff)
- django/trunk/docs/model-api.txt (modified) (1 diff)
- 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/base.py
r6269 r6346 83 83 def _get_pk_val(self): 84 84 return getattr(self, self._meta.pk.attname) 85 86 def _set_pk_val(self, value): 87 return setattr(self, self._meta.pk.attname, value) 88 89 pk = property(_get_pk_val, _set_pk_val) 85 90 86 91 def __repr__(self): django/trunk/docs/model-api.txt
r6304 r6346 1284 1284 1285 1285 Each model requires exactly one field to have ``primary_key=True``. 1286 1287 The ``pk`` property 1288 ------------------- 1289 **New in Django development version** 1290 1291 Regardless of whether you define a primary key field yourself, or let Django 1292 supply one for you, each model will have a property called ``pk``. It behaves 1293 like a normal attribute on the model, but is actually an alias for whichever 1294 attribute is the primary key field for the model. You can read and set this 1295 value, just as you would for any other attribute, and it will update the 1296 correct field in the model. 1286 1297 1287 1298 Admin options django/trunk/tests/modeltests/basic/models.py
r5876 r6346 32 32 # Now it has an ID. Note it's a long integer, as designated by the trailing "L". 33 33 >>> a.id 34 1L 35 36 # Models have a pk property that is an alias for the primary key attribute (by 37 # default, the 'id' attribute). 38 >>> a.pk 34 39 1L 35 40 django/trunk/tests/modeltests/custom_pk/models.py
r6200 r6346 57 57 [<Employee: Fran Bones>, <Employee: Dan Jones>] 58 58 59 # The primary key can be accessed via the pk property on the model. 60 >>> e = Employee.objects.get(pk='ABC123') 61 >>> e.pk 62 u'ABC123' 63 64 # Or we can use the real attribute name for the primary key: 65 >>> e.employee_code 66 u'ABC123' 67 59 68 # Fran got married and changed her last name. 60 69 >>> fran = Employee.objects.get(pk='XYZ456')
