1 """ |
|
2 14. Using a custom primary key |
|
3 |
|
4 By default, Django adds an ``"id"`` field to each model. But you can override |
|
5 this behavior by explicitly adding ``primary_key=True`` to a field. |
|
6 """ |
|
7 |
|
8 from django.db import models |
|
9 |
|
10 class Employee(models.Model): |
|
11 employee_code = models.CharField(maxlength=10, primary_key=True, |
|
12 db_column = 'code') |
|
13 first_name = models.CharField(maxlength=20) |
|
14 last_name = models.CharField(maxlength=20) |
|
15 class Meta: |
|
16 ordering = ('last_name', 'first_name') |
|
17 |
|
18 def __str__(self): |
|
19 return "%s %s" % (self.first_name, self.last_name) |
|
20 |
|
21 class Business(models.Model): |
|
22 name = models.CharField(maxlength=20, primary_key=True) |
|
23 employees = models.ManyToManyField(Employee) |
|
24 class Meta: |
|
25 verbose_name_plural = 'businesses' |
|
26 |
|
27 def __str__(self): |
|
28 return self.name |
|
29 |
|
30 __test__ = {'API_TESTS':""" |
|
31 >>> dan = Employee(employee_code='ABC123', first_name='Dan', last_name='Jones') |
|
32 >>> dan.save() |
|
33 >>> Employee.objects.all() |
|
34 [<Employee: Dan Jones>] |
|
35 |
|
36 >>> fran = Employee(employee_code='XYZ456', first_name='Fran', last_name='Bones') |
|
37 >>> fran.save() |
|
38 >>> Employee.objects.all() |
|
39 [<Employee: Fran Bones>, <Employee: Dan Jones>] |
|
40 |
|
41 >>> Employee.objects.get(pk='ABC123') |
|
42 <Employee: Dan Jones> |
|
43 >>> Employee.objects.get(pk='XYZ456') |
|
44 <Employee: Fran Bones> |
|
45 >>> Employee.objects.get(pk='foo') |
|
46 Traceback (most recent call last): |
|
47 ... |
|
48 DoesNotExist: Employee matching query does not exist. |
|
49 |
|
50 # Use the name of the primary key, rather than pk. |
|
51 >>> Employee.objects.get(employee_code__exact='ABC123') |
|
52 <Employee: Dan Jones> |
|
53 |
|
54 # pk can be used as a substitute for the primary key. |
|
55 >>> Employee.objects.filter(pk__in=['ABC123','XYZ456']) |
|
56 [<Employee: Fran Bones>, <Employee: Dan Jones>] |
|
57 |
|
58 # Fran got married and changed her last name. |
|
59 >>> fran = Employee.objects.get(pk='XYZ456') |
|
60 >>> fran.last_name = 'Jones' |
|
61 >>> fran.save() |
|
62 >>> Employee.objects.filter(last_name__exact='Jones') |
|
63 [<Employee: Dan Jones>, <Employee: Fran Jones>] |
|
64 >>> Employee.objects.in_bulk(['ABC123', 'XYZ456']) |
|
65 {'XYZ456': <Employee: Fran Jones>, 'ABC123': <Employee: Dan Jones>} |
|
66 |
|
67 >>> b = Business(name='Sears') |
|
68 >>> b.save() |
|
69 >>> b.employees.add(dan, fran) |
|
70 >>> b.employees.all() |
|
71 [<Employee: Dan Jones>, <Employee: Fran Jones>] |
|
72 >>> fran.business_set.all() |
|
73 [<Business: Sears>] |
|
74 >>> Business.objects.in_bulk(['Sears']) |
|
75 {'Sears': <Business: Sears>} |
|
76 |
|
77 >>> Business.objects.filter(name__exact='Sears') |
|
78 [<Business: Sears>] |
|
79 >>> Business.objects.filter(pk='Sears') |
|
80 [<Business: Sears>] |
|
81 |
|
82 # Queries across tables, involving primary key |
|
83 >>> Employee.objects.filter(business__name__exact='Sears') |
|
84 [<Employee: Dan Jones>, <Employee: Fran Jones>] |
|
85 >>> Employee.objects.filter(business__pk='Sears') |
|
86 [<Employee: Dan Jones>, <Employee: Fran Jones>] |
|
87 |
|
88 >>> Business.objects.filter(employees__employee_code__exact='ABC123') |
|
89 [<Business: Sears>] |
|
90 >>> Business.objects.filter(employees__pk='ABC123') |
|
91 [<Business: Sears>] |
|
92 >>> Business.objects.filter(employees__first_name__startswith='Fran') |
|
93 [<Business: Sears>] |
|
94 |
|
95 """} |
|