2006-05-02 09:31:56 +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.db import models
|
|
|
|
|
|
|
|
class Employee(models.Model):
|
2006-06-15 19:28:28 +08:00
|
|
|
employee_code = models.CharField(maxlength=10, primary_key=True,
|
|
|
|
db_column = 'code')
|
2006-05-02 09:31:56 +08:00
|
|
|
first_name = models.CharField(maxlength=20)
|
|
|
|
last_name = models.CharField(maxlength=20)
|
|
|
|
class Meta:
|
|
|
|
ordering = ('last_name', 'first_name')
|
|
|
|
|
2006-06-04 08:23:51 +08:00
|
|
|
def __str__(self):
|
2006-05-02 09:31:56 +08:00
|
|
|
return "%s %s" % (self.first_name, self.last_name)
|
|
|
|
|
|
|
|
class Business(models.Model):
|
|
|
|
name = models.CharField(maxlength=20, primary_key=True)
|
|
|
|
employees = models.ManyToManyField(Employee)
|
|
|
|
class Meta:
|
|
|
|
verbose_name_plural = 'businesses'
|
|
|
|
|
2006-06-04 08:23:51 +08:00
|
|
|
def __str__(self):
|
2006-05-02 09:31:56 +08:00
|
|
|
return self.name
|
|
|
|
|
2006-08-27 21:59:47 +08:00
|
|
|
__test__ = {'API_TESTS':"""
|
2006-05-02 09:31:56 +08:00
|
|
|
>>> dan = Employee(employee_code='ABC123', first_name='Dan', last_name='Jones')
|
|
|
|
>>> dan.save()
|
|
|
|
>>> Employee.objects.all()
|
2006-06-04 08:23:51 +08:00
|
|
|
[<Employee: Dan Jones>]
|
2006-05-02 09:31:56 +08:00
|
|
|
|
|
|
|
>>> fran = Employee(employee_code='XYZ456', first_name='Fran', last_name='Bones')
|
|
|
|
>>> fran.save()
|
|
|
|
>>> Employee.objects.all()
|
2006-06-04 08:23:51 +08:00
|
|
|
[<Employee: Fran Bones>, <Employee: Dan Jones>]
|
2006-05-02 09:31:56 +08:00
|
|
|
|
|
|
|
>>> Employee.objects.get(pk='ABC123')
|
2006-06-04 08:23:51 +08:00
|
|
|
<Employee: Dan Jones>
|
2006-05-02 09:31:56 +08:00
|
|
|
>>> Employee.objects.get(pk='XYZ456')
|
2006-06-04 08:23:51 +08:00
|
|
|
<Employee: Fran Bones>
|
2006-05-02 09:31:56 +08:00
|
|
|
>>> Employee.objects.get(pk='foo')
|
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
2006-05-07 02:36:32 +08:00
|
|
|
DoesNotExist: Employee matching query does not exist.
|
2006-05-02 09:31:56 +08:00
|
|
|
|
|
|
|
# Use the name of the primary key, rather than pk.
|
|
|
|
>>> Employee.objects.get(employee_code__exact='ABC123')
|
2006-06-04 08:23:51 +08:00
|
|
|
<Employee: Dan Jones>
|
2006-05-02 09:31:56 +08:00
|
|
|
|
|
|
|
# Fran got married and changed her last name.
|
|
|
|
>>> fran = Employee.objects.get(pk='XYZ456')
|
|
|
|
>>> fran.last_name = 'Jones'
|
|
|
|
>>> fran.save()
|
|
|
|
>>> Employee.objects.filter(last_name__exact='Jones')
|
2006-06-04 08:23:51 +08:00
|
|
|
[<Employee: Dan Jones>, <Employee: Fran Jones>]
|
2006-05-02 09:31:56 +08:00
|
|
|
>>> Employee.objects.in_bulk(['ABC123', 'XYZ456'])
|
2006-06-04 08:23:51 +08:00
|
|
|
{'XYZ456': <Employee: Fran Jones>, 'ABC123': <Employee: Dan Jones>}
|
2006-05-02 09:31:56 +08:00
|
|
|
|
|
|
|
>>> b = Business(name='Sears')
|
|
|
|
>>> b.save()
|
|
|
|
>>> b.employees.add(dan, fran)
|
|
|
|
>>> b.employees.all()
|
2006-06-04 08:23:51 +08:00
|
|
|
[<Employee: Dan Jones>, <Employee: Fran Jones>]
|
2006-05-02 09:31:56 +08:00
|
|
|
>>> fran.business_set.all()
|
2006-06-04 08:23:51 +08:00
|
|
|
[<Business: Sears>]
|
2006-05-02 09:31:56 +08:00
|
|
|
>>> Business.objects.in_bulk(['Sears'])
|
2006-06-04 08:23:51 +08:00
|
|
|
{'Sears': <Business: Sears>}
|
2006-05-02 09:31:56 +08:00
|
|
|
|
|
|
|
>>> Business.objects.filter(name__exact='Sears')
|
2006-06-04 08:23:51 +08:00
|
|
|
[<Business: Sears>]
|
2006-05-02 09:31:56 +08:00
|
|
|
>>> Business.objects.filter(pk='Sears')
|
2006-06-04 08:23:51 +08:00
|
|
|
[<Business: Sears>]
|
2006-05-02 09:31:56 +08:00
|
|
|
|
|
|
|
# Queries across tables, involving primary key
|
|
|
|
>>> Employee.objects.filter(business__name__exact='Sears')
|
2006-06-04 08:23:51 +08:00
|
|
|
[<Employee: Dan Jones>, <Employee: Fran Jones>]
|
2006-05-02 09:31:56 +08:00
|
|
|
>>> Employee.objects.filter(business__pk='Sears')
|
2006-06-04 08:23:51 +08:00
|
|
|
[<Employee: Dan Jones>, <Employee: Fran Jones>]
|
2006-05-02 09:31:56 +08:00
|
|
|
|
|
|
|
>>> Business.objects.filter(employees__employee_code__exact='ABC123')
|
2006-06-04 08:23:51 +08:00
|
|
|
[<Business: Sears>]
|
2006-05-02 09:31:56 +08:00
|
|
|
>>> Business.objects.filter(employees__pk='ABC123')
|
2006-06-04 08:23:51 +08:00
|
|
|
[<Business: Sears>]
|
2006-05-02 09:31:56 +08:00
|
|
|
>>> Business.objects.filter(employees__first_name__startswith='Fran')
|
2006-06-04 08:23:51 +08:00
|
|
|
[<Business: Sears>]
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2006-08-27 21:59:47 +08:00
|
|
|
"""}
|