2005-08-10 11:52:41 +08:00
|
|
|
"""
|
|
|
|
14. Using a custom primary key
|
|
|
|
|
|
|
|
By default, Django adds an ``"id"`` field to each model. But you can override
|
|
|
|
this behavior by explicitly adding ``primary_key=True`` to a field.
|
|
|
|
"""
|
|
|
|
|
|
|
|
from django.core import meta
|
|
|
|
|
|
|
|
class Employee(meta.Model):
|
2005-08-26 06:51:30 +08:00
|
|
|
employee_code = meta.CharField(maxlength=10, primary_key=True)
|
|
|
|
first_name = meta.CharField(maxlength=20)
|
|
|
|
last_name = meta.CharField(maxlength=20)
|
|
|
|
class META:
|
|
|
|
ordering = ('last_name', 'first_name')
|
2005-08-10 11:52:41 +08:00
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return "%s %s" % (self.first_name, self.last_name)
|
|
|
|
|
2005-09-26 02:29:59 +08:00
|
|
|
class Business(meta.Model):
|
|
|
|
name = meta.CharField(maxlength=20, primary_key=True)
|
|
|
|
employees = meta.ManyToManyField(Employee)
|
|
|
|
class META:
|
|
|
|
verbose_name_plural = 'businesses'
|
|
|
|
module_name = 'businesses'
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return self.name
|
|
|
|
|
2005-08-10 11:52:41 +08:00
|
|
|
API_TESTS = """
|
2005-08-10 13:04:27 +08:00
|
|
|
>>> dan = employees.Employee(employee_code='ABC123', first_name='Dan', last_name='Jones')
|
|
|
|
>>> dan.save()
|
2005-08-10 11:52:41 +08:00
|
|
|
>>> employees.get_list()
|
|
|
|
[Dan Jones]
|
2005-08-10 13:04:27 +08:00
|
|
|
|
|
|
|
>>> fran = employees.Employee(employee_code='XYZ456', first_name='Fran', last_name='Bones')
|
|
|
|
>>> fran.save()
|
|
|
|
>>> employees.get_list()
|
|
|
|
[Fran Bones, Dan Jones]
|
|
|
|
|
|
|
|
>>> employees.get_object(pk='ABC123')
|
|
|
|
Dan Jones
|
|
|
|
>>> employees.get_object(pk='XYZ456')
|
|
|
|
Fran Bones
|
|
|
|
>>> employees.get_object(pk='foo')
|
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
|
|
|
EmployeeDoesNotExist: Employee does not exist for {'pk': 'foo'}
|
|
|
|
|
|
|
|
# Fran got married and changed her last name.
|
|
|
|
>>> fran = employees.get_object(pk='XYZ456')
|
|
|
|
>>> fran.last_name = 'Jones'
|
|
|
|
>>> fran.save()
|
|
|
|
>>> employees.get_list(last_name__exact='Jones')
|
|
|
|
[Dan Jones, Fran Jones]
|
2005-09-26 02:29:59 +08:00
|
|
|
|
|
|
|
>>> b = businesses.Business(name='Sears')
|
|
|
|
>>> b.save()
|
|
|
|
>>> b.set_employees([dan.employee_code, fran.employee_code])
|
|
|
|
True
|
|
|
|
>>> b.get_employee_list()
|
|
|
|
[Dan Jones, Fran Jones]
|
|
|
|
>>> fran.get_business_list()
|
|
|
|
[Sears]
|
2005-08-10 11:52:41 +08:00
|
|
|
"""
|