| 1 |
# -*- coding: utf-8 -*- |
|---|
| 2 |
""" |
|---|
| 3 |
14. Using a custom primary key |
|---|
| 4 |
|
|---|
| 5 |
By default, Django adds an ``"id"`` field to each model. But you can override |
|---|
| 6 |
this behavior by explicitly adding ``primary_key=True`` to a field. |
|---|
| 7 |
""" |
|---|
| 8 |
|
|---|
| 9 |
from django.conf import settings |
|---|
| 10 |
from django.db import models, transaction, IntegrityError |
|---|
| 11 |
|
|---|
| 12 |
class Employee(models.Model): |
|---|
| 13 |
employee_code = models.IntegerField(primary_key=True, db_column = 'code') |
|---|
| 14 |
first_name = models.CharField(max_length=20) |
|---|
| 15 |
last_name = models.CharField(max_length=20) |
|---|
| 16 |
class Meta: |
|---|
| 17 |
ordering = ('last_name', 'first_name') |
|---|
| 18 |
|
|---|
| 19 |
def __unicode__(self): |
|---|
| 20 |
return u"%s %s" % (self.first_name, self.last_name) |
|---|
| 21 |
|
|---|
| 22 |
class Business(models.Model): |
|---|
| 23 |
name = models.CharField(max_length=20, primary_key=True) |
|---|
| 24 |
employees = models.ManyToManyField(Employee) |
|---|
| 25 |
class Meta: |
|---|
| 26 |
verbose_name_plural = 'businesses' |
|---|
| 27 |
|
|---|
| 28 |
def __unicode__(self): |
|---|
| 29 |
return self.name |
|---|
| 30 |
|
|---|
| 31 |
__test__ = {'API_TESTS':""" |
|---|
| 32 |
>>> dan = Employee(employee_code=123, first_name='Dan', last_name='Jones') |
|---|
| 33 |
>>> dan.save() |
|---|
| 34 |
>>> Employee.objects.all() |
|---|
| 35 |
[<Employee: Dan Jones>] |
|---|
| 36 |
|
|---|
| 37 |
>>> fran = Employee(employee_code=456, first_name='Fran', last_name='Bones') |
|---|
| 38 |
>>> fran.save() |
|---|
| 39 |
>>> Employee.objects.all() |
|---|
| 40 |
[<Employee: Fran Bones>, <Employee: Dan Jones>] |
|---|
| 41 |
|
|---|
| 42 |
>>> Employee.objects.get(pk=123) |
|---|
| 43 |
<Employee: Dan Jones> |
|---|
| 44 |
>>> Employee.objects.get(pk=456) |
|---|
| 45 |
<Employee: Fran Bones> |
|---|
| 46 |
>>> Employee.objects.get(pk=42) |
|---|
| 47 |
Traceback (most recent call last): |
|---|
| 48 |
... |
|---|
| 49 |
DoesNotExist: Employee matching query does not exist. |
|---|
| 50 |
|
|---|
| 51 |
# Use the name of the primary key, rather than pk. |
|---|
| 52 |
>>> Employee.objects.get(employee_code__exact=123) |
|---|
| 53 |
<Employee: Dan Jones> |
|---|
| 54 |
|
|---|
| 55 |
# pk can be used as a substitute for the primary key. |
|---|
| 56 |
>>> Employee.objects.filter(pk__in=[123, 456]) |
|---|
| 57 |
[<Employee: Fran Bones>, <Employee: Dan Jones>] |
|---|
| 58 |
|
|---|
| 59 |
# The primary key can be accessed via the pk property on the model. |
|---|
| 60 |
>>> e = Employee.objects.get(pk=123) |
|---|
| 61 |
>>> e.pk |
|---|
| 62 |
123 |
|---|
| 63 |
|
|---|
| 64 |
# Or we can use the real attribute name for the primary key: |
|---|
| 65 |
>>> e.employee_code |
|---|
| 66 |
123 |
|---|
| 67 |
|
|---|
| 68 |
# Fran got married and changed her last name. |
|---|
| 69 |
>>> fran = Employee.objects.get(pk=456) |
|---|
| 70 |
>>> fran.last_name = 'Jones' |
|---|
| 71 |
>>> fran.save() |
|---|
| 72 |
>>> Employee.objects.filter(last_name__exact='Jones') |
|---|
| 73 |
[<Employee: Dan Jones>, <Employee: Fran Jones>] |
|---|
| 74 |
>>> emps = Employee.objects.in_bulk([123, 456]) |
|---|
| 75 |
>>> emps[123] |
|---|
| 76 |
<Employee: Dan Jones> |
|---|
| 77 |
|
|---|
| 78 |
>>> b = Business(name='Sears') |
|---|
| 79 |
>>> b.save() |
|---|
| 80 |
>>> b.employees.add(dan, fran) |
|---|
| 81 |
>>> b.employees.all() |
|---|
| 82 |
[<Employee: Dan Jones>, <Employee: Fran Jones>] |
|---|
| 83 |
>>> fran.business_set.all() |
|---|
| 84 |
[<Business: Sears>] |
|---|
| 85 |
>>> Business.objects.in_bulk(['Sears']) |
|---|
| 86 |
{u'Sears': <Business: Sears>} |
|---|
| 87 |
|
|---|
| 88 |
>>> Business.objects.filter(name__exact='Sears') |
|---|
| 89 |
[<Business: Sears>] |
|---|
| 90 |
>>> Business.objects.filter(pk='Sears') |
|---|
| 91 |
[<Business: Sears>] |
|---|
| 92 |
|
|---|
| 93 |
# Queries across tables, involving primary key |
|---|
| 94 |
>>> Employee.objects.filter(business__name__exact='Sears') |
|---|
| 95 |
[<Employee: Dan Jones>, <Employee: Fran Jones>] |
|---|
| 96 |
>>> Employee.objects.filter(business__pk='Sears') |
|---|
| 97 |
[<Employee: Dan Jones>, <Employee: Fran Jones>] |
|---|
| 98 |
|
|---|
| 99 |
>>> Business.objects.filter(employees__employee_code__exact=123) |
|---|
| 100 |
[<Business: Sears>] |
|---|
| 101 |
>>> Business.objects.filter(employees__pk=123) |
|---|
| 102 |
[<Business: Sears>] |
|---|
| 103 |
>>> Business.objects.filter(employees__first_name__startswith='Fran') |
|---|
| 104 |
[<Business: Sears>] |
|---|
| 105 |
|
|---|
| 106 |
# Primary key may be unicode string |
|---|
| 107 |
>>> bus = Business(name=u'jaźń') |
|---|
| 108 |
>>> bus.save() |
|---|
| 109 |
|
|---|
| 110 |
# The primary key must also obviously be unique, so trying to create a new |
|---|
| 111 |
# object with the same primary key will fail. |
|---|
| 112 |
>>> try: |
|---|
| 113 |
... sid = transaction.savepoint() |
|---|
| 114 |
... Employee.objects.create(employee_code=123, first_name='Fred', last_name='Jones') |
|---|
| 115 |
... transaction.savepoint_commit(sid) |
|---|
| 116 |
... except Exception, e: |
|---|
| 117 |
... if isinstance(e, IntegrityError): |
|---|
| 118 |
... transaction.savepoint_rollback(sid) |
|---|
| 119 |
... print "Pass" |
|---|
| 120 |
... else: |
|---|
| 121 |
... print "Fail with %s" % type(e) |
|---|
| 122 |
Pass |
|---|
| 123 |
|
|---|
| 124 |
"""} |
|---|
| 125 |
|
|---|
| 126 |
# SQLite lets objects be saved with an empty primary key, even though an |
|---|
| 127 |
# integer is expected. So we can't check for an error being raised in that case |
|---|
| 128 |
# for SQLite. Remove it from the suite for this next bit. |
|---|
| 129 |
if settings.DATABASE_ENGINE != 'sqlite3': |
|---|
| 130 |
__test__["API_TESTS"] += """ |
|---|
| 131 |
# The primary key must be specified, so an error is raised if you try to create |
|---|
| 132 |
# an object without it. |
|---|
| 133 |
>>> try: |
|---|
| 134 |
... sid = transaction.savepoint() |
|---|
| 135 |
... Employee.objects.create(first_name='Tom', last_name='Smith') |
|---|
| 136 |
... print 'hello' |
|---|
| 137 |
... transaction.savepoint_commit(sid) |
|---|
| 138 |
... print 'hello2' |
|---|
| 139 |
... except Exception, e: |
|---|
| 140 |
... if isinstance(e, IntegrityError): |
|---|
| 141 |
... transaction.savepoint_rollback(sid) |
|---|
| 142 |
... print "Pass" |
|---|
| 143 |
... else: |
|---|
| 144 |
... print "Fail with %s" % type(e) |
|---|
| 145 |
Pass |
|---|
| 146 |
|
|---|
| 147 |
""" |
|---|