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.
|
|
|
|
"""
|
|
|
|
|
2005-12-14 13:02:51 +08:00
|
|
|
from django.db import models
|
2005-08-10 11:52:41 +08:00
|
|
|
|
2005-12-14 13:02:51 +08:00
|
|
|
class Employee(models.Model):
|
|
|
|
employee_code = models.CharField(maxlength=10, primary_key=True)
|
|
|
|
first_name = models.CharField(maxlength=20)
|
|
|
|
last_name = models.CharField(maxlength=20)
|
2005-12-21 10:42:27 +08:00
|
|
|
class Meta:
|
2005-08-26 06:51:30 +08:00
|
|
|
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-12-14 13:02:51 +08:00
|
|
|
class Business(models.Model):
|
|
|
|
name = models.CharField(maxlength=20, primary_key=True)
|
|
|
|
employees = models.ManyToManyField(Employee)
|
2005-12-21 10:42:27 +08:00
|
|
|
class Meta:
|
2005-09-26 02:29:59 +08:00
|
|
|
verbose_name_plural = 'businesses'
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return self.name
|
|
|
|
|
2005-08-10 11:52:41 +08:00
|
|
|
API_TESTS = """
|
2005-12-12 06:10:02 +08:00
|
|
|
>>> dan = Employee(employee_code='ABC123', first_name='Dan', last_name='Jones')
|
2005-08-10 13:04:27 +08:00
|
|
|
>>> dan.save()
|
2005-12-12 06:10:02 +08:00
|
|
|
>>> Employee.objects.get_list()
|
2005-08-10 11:52:41 +08:00
|
|
|
[Dan Jones]
|
2005-08-10 13:04:27 +08:00
|
|
|
|
2005-12-12 06:10:02 +08:00
|
|
|
>>> fran = Employee(employee_code='XYZ456', first_name='Fran', last_name='Bones')
|
2005-08-10 13:04:27 +08:00
|
|
|
>>> fran.save()
|
2005-12-12 06:10:02 +08:00
|
|
|
>>> Employee.objects.get_list()
|
2005-08-10 13:04:27 +08:00
|
|
|
[Fran Bones, Dan Jones]
|
|
|
|
|
2005-12-12 06:10:02 +08:00
|
|
|
>>> Employee.objects.get_object(pk='ABC123')
|
2005-08-10 13:04:27 +08:00
|
|
|
Dan Jones
|
2005-12-12 06:10:02 +08:00
|
|
|
>>> Employee.objects.get_object(pk='XYZ456')
|
2005-08-10 13:04:27 +08:00
|
|
|
Fran Bones
|
2005-12-12 06:10:02 +08:00
|
|
|
>>> Employee.objects.get_object(pk='foo')
|
2005-08-10 13:04:27 +08:00
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
2005-12-15 05:50:37 +08:00
|
|
|
DoesNotExist: Employee does not exist for {'pk': 'foo'}
|
2005-08-10 13:04:27 +08:00
|
|
|
|
2006-01-08 14:16:05 +08:00
|
|
|
# Use the name of the primary key, rather than pk.
|
|
|
|
>>> Employee.objects.get_object(employee_code__exact='ABC123')
|
|
|
|
Dan Jones
|
|
|
|
|
2005-08-10 13:04:27 +08:00
|
|
|
# Fran got married and changed her last name.
|
2005-12-12 06:10:02 +08:00
|
|
|
>>> fran = Employee.objects.get_object(pk='XYZ456')
|
2005-08-10 13:04:27 +08:00
|
|
|
>>> fran.last_name = 'Jones'
|
|
|
|
>>> fran.save()
|
2005-12-12 06:10:02 +08:00
|
|
|
>>> Employee.objects.get_list(last_name__exact='Jones')
|
2005-08-10 13:04:27 +08:00
|
|
|
[Dan Jones, Fran Jones]
|
2005-12-12 06:10:02 +08:00
|
|
|
>>> Employee.objects.get_in_bulk(['ABC123', 'XYZ456'])
|
2005-10-23 05:18:53 +08:00
|
|
|
{'XYZ456': Fran Jones, 'ABC123': Dan Jones}
|
2005-09-26 02:29:59 +08:00
|
|
|
|
2005-12-12 06:10:02 +08:00
|
|
|
>>> b = Business(name='Sears')
|
2005-09-26 02:29:59 +08:00
|
|
|
>>> 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-12-12 06:10:02 +08:00
|
|
|
>>> Business.objects.get_in_bulk(['Sears'])
|
2005-10-23 05:18:53 +08:00
|
|
|
{'Sears': Sears}
|
2006-01-08 14:16:05 +08:00
|
|
|
|
|
|
|
>>> Business.objects.get_list(name__exact='Sears')
|
|
|
|
[Sears]
|
|
|
|
>>> Business.objects.get_list(pk='Sears')
|
|
|
|
[Sears]
|
|
|
|
|
|
|
|
# Queries across tables, involving primary key
|
2006-01-26 05:51:13 +08:00
|
|
|
>>> Employee.objects.get_list(business__name__exact='Sears')
|
2006-01-08 14:16:05 +08:00
|
|
|
[Dan Jones, Fran Jones]
|
2006-01-26 05:51:13 +08:00
|
|
|
>>> Employee.objects.get_list(business__pk='Sears')
|
2006-01-08 14:16:05 +08:00
|
|
|
[Dan Jones, Fran Jones]
|
|
|
|
|
|
|
|
>>> Business.objects.get_list(employees__employee_code__exact='ABC123')
|
|
|
|
[Sears]
|
|
|
|
>>> Business.objects.get_list(employees__pk='ABC123')
|
|
|
|
[Sears]
|
|
|
|
>>> Business.objects.get_list(employees__first_name__startswith='Fran')
|
|
|
|
[Sears]
|
|
|
|
|
2005-08-10 11:52:41 +08:00
|
|
|
"""
|