Ticket #3703: pk_property_with_tests.diff
File pk_property_with_tests.diff, 1.6 KB (added by , 17 years ago) |
---|
-
django/db/models/base.py
83 83 def _get_pk_val(self): 84 84 return getattr(self, self._meta.pk.attname) 85 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) 90 86 91 def __repr__(self): 87 92 return smart_str(u'<%s: %s>' % (self.__class__.__name__, unicode(self))) 88 93 -
tests/modeltests/basic/models.py
33 33 >>> a.id 34 34 1L 35 35 36 # New in Django development version: 37 # Models have a pk attribute that points to the primary key 38 # By default this is the id 39 >>> a.pk 40 1L 41 36 42 # Access database columns via Python attributes. 37 43 >>> a.headline 38 44 'Area man programs in Python' -
tests/modeltests/custom_pk/models.py
56 56 >>> Employee.objects.filter(pk__in=['ABC123','XYZ456']) 57 57 [<Employee: Fran Bones>, <Employee: Dan Jones>] 58 58 59 # New in Django development version: 60 # pk can be accessed like any other attribute 61 >>> e = Employee.objects.get(pk='ABC123') 62 >>> e.pk 63 u'ABC123' 64 59 65 # Fran got married and changed her last name. 60 66 >>> fran = Employee.objects.get(pk='XYZ456') 61 67 >>> fran.last_name = 'Jones'