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