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()
|
2006-01-31 09:08:02 +08:00
|
|
|
>>> Employee.objects.all()
|
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()
|
2006-01-31 09:08:02 +08:00
|
|
|
>>> Employee.objects.all()
|
2005-08-10 13:04:27 +08:00
|
|
|
[Fran Bones, Dan Jones]
|
|
|
|
|
2006-01-30 08:38:23 +08:00
|
|
|
>>> Employee.objects.get(pk='ABC123')
|
2005-08-10 13:04:27 +08:00
|
|
|
Dan Jones
|
2006-01-30 08:38:23 +08:00
|
|
|
>>> Employee.objects.get(pk='XYZ456')
|
2005-08-10 13:04:27 +08:00
|
|
|
Fran Bones
|
2006-01-30 08:38:23 +08:00
|
|
|
>>> Employee.objects.get(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.
|
2006-01-30 08:38:23 +08:00
|
|
|
>>> Employee.objects.get(employee_code__exact='ABC123')
|
2006-01-08 14:16:05 +08:00
|
|
|
Dan Jones
|
|
|
|
|
2005-08-10 13:04:27 +08:00
|
|
|
# Fran got married and changed her last name.
|
2006-01-30 08:38:23 +08:00
|
|
|
>>> fran = Employee.objects.get(pk='XYZ456')
|
2005-08-10 13:04:27 +08:00
|
|
|
>>> fran.last_name = 'Jones'
|
|
|
|
>>> fran.save()
|
2006-01-31 09:08:02 +08:00
|
|
|
>>> Employee.objects.filter(last_name__exact='Jones')
|
2005-08-10 13:04:27 +08:00
|
|
|
[Dan Jones, Fran Jones]
|
2006-01-30 08:38:23 +08:00
|
|
|
>>> Employee.objects.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
|
2006-01-31 09:08:02 +08:00
|
|
|
>>> b.employee_set.all()
|
2005-09-26 02:29:59 +08:00
|
|
|
[Dan Jones, Fran Jones]
|
2006-01-31 09:08:02 +08:00
|
|
|
>>> fran.business_set.all()
|
2005-09-26 02:29:59 +08:00
|
|
|
[Sears]
|
2006-01-30 08:38:23 +08:00
|
|
|
>>> Business.objects.in_bulk(['Sears'])
|
2005-10-23 05:18:53 +08:00
|
|
|
{'Sears': Sears}
|
2006-01-08 14:16:05 +08:00
|
|
|
|
2006-01-31 09:08:02 +08:00
|
|
|
>>> Business.objects.filter(name__exact='Sears')
|
2006-01-08 14:16:05 +08:00
|
|
|
[Sears]
|
2006-01-31 09:08:02 +08:00
|
|
|
>>> Business.objects.filter(pk='Sears')
|
2006-01-08 14:16:05 +08:00
|
|
|
[Sears]
|
|
|
|
|
|
|
|
# Queries across tables, involving primary key
|
2006-01-31 09:08:02 +08:00
|
|
|
>>> Employee.objects.filter(business__name__exact='Sears')
|
2006-01-08 14:16:05 +08:00
|
|
|
[Dan Jones, Fran Jones]
|
2006-01-31 09:08:02 +08:00
|
|
|
>>> Employee.objects.filter(business__pk='Sears')
|
2006-01-08 14:16:05 +08:00
|
|
|
[Dan Jones, Fran Jones]
|
|
|
|
|
2006-01-31 09:08:02 +08:00
|
|
|
>>> Business.objects.filter(employees__employee_code__exact='ABC123')
|
2006-01-08 14:16:05 +08:00
|
|
|
[Sears]
|
2006-01-31 09:08:02 +08:00
|
|
|
>>> Business.objects.filter(employees__pk='ABC123')
|
2006-01-08 14:16:05 +08:00
|
|
|
[Sears]
|
2006-01-31 09:08:02 +08:00
|
|
|
>>> Business.objects.filter(employees__first_name__startswith='Fran')
|
2006-01-08 14:16:05 +08:00
|
|
|
[Sears]
|
|
|
|
|
2005-08-10 11:52:41 +08:00
|
|
|
"""
|