~kkubasik/django/aggregation-branch

« back to all changes in this revision

Viewing changes to tests/modeltests/custom_pk/models.py

  • Committer: adrian
  • Date: 2006-05-02 01:31:56 UTC
  • Revision ID: vcs-imports@canonical.com-20060502013156-2941fcd40d080649
MERGED MAGIC-REMOVAL BRANCH TO TRUNK. This change is highly backwards-incompatible. Please read http://code.djangoproject.com/wiki/RemovingTheMagic for upgrade instructions.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
    first_name = models.CharField(maxlength=20)
 
13
    last_name = models.CharField(maxlength=20)
 
14
    class Meta:
 
15
        ordering = ('last_name', 'first_name')
 
16
 
 
17
    def __repr__(self):
 
18
        return "%s %s" % (self.first_name, self.last_name)
 
19
 
 
20
class Business(models.Model):
 
21
    name = models.CharField(maxlength=20, primary_key=True)
 
22
    employees = models.ManyToManyField(Employee)
 
23
    class Meta:
 
24
        verbose_name_plural = 'businesses'
 
25
 
 
26
    def __repr__(self):
 
27
        return self.name
 
28
 
 
29
API_TESTS = """
 
30
>>> dan = Employee(employee_code='ABC123', first_name='Dan', last_name='Jones')
 
31
>>> dan.save()
 
32
>>> Employee.objects.all()
 
33
[Dan Jones]
 
34
 
 
35
>>> fran = Employee(employee_code='XYZ456', first_name='Fran', last_name='Bones')
 
36
>>> fran.save()
 
37
>>> Employee.objects.all()
 
38
[Fran Bones, Dan Jones]
 
39
 
 
40
>>> Employee.objects.get(pk='ABC123')
 
41
Dan Jones
 
42
>>> Employee.objects.get(pk='XYZ456')
 
43
Fran Bones
 
44
>>> Employee.objects.get(pk='foo')
 
45
Traceback (most recent call last):
 
46
    ...
 
47
DoesNotExist: Employee does not exist for {'pk': 'foo'}
 
48
 
 
49
# Use the name of the primary key, rather than pk.
 
50
>>> Employee.objects.get(employee_code__exact='ABC123')
 
51
Dan Jones
 
52
 
 
53
# Fran got married and changed her last name.
 
54
>>> fran = Employee.objects.get(pk='XYZ456')
 
55
>>> fran.last_name = 'Jones'
 
56
>>> fran.save()
 
57
>>> Employee.objects.filter(last_name__exact='Jones')
 
58
[Dan Jones, Fran Jones]
 
59
>>> Employee.objects.in_bulk(['ABC123', 'XYZ456'])
 
60
{'XYZ456': Fran Jones, 'ABC123': Dan Jones}
 
61
 
 
62
>>> b = Business(name='Sears')
 
63
>>> b.save()
 
64
>>> b.employees.add(dan, fran)
 
65
>>> b.employees.all()
 
66
[Dan Jones, Fran Jones]
 
67
>>> fran.business_set.all()
 
68
[Sears]
 
69
>>> Business.objects.in_bulk(['Sears'])
 
70
{'Sears': Sears}
 
71
 
 
72
>>> Business.objects.filter(name__exact='Sears')
 
73
[Sears]
 
74
>>> Business.objects.filter(pk='Sears')
 
75
[Sears]
 
76
 
 
77
# Queries across tables, involving primary key
 
78
>>> Employee.objects.filter(business__name__exact='Sears')
 
79
[Dan Jones, Fran Jones]
 
80
>>> Employee.objects.filter(business__pk='Sears')
 
81
[Dan Jones, Fran Jones]
 
82
 
 
83
>>> Business.objects.filter(employees__employee_code__exact='ABC123')
 
84
[Sears]
 
85
>>> Business.objects.filter(employees__pk='ABC123')
 
86
[Sears]
 
87
>>> Business.objects.filter(employees__first_name__startswith='Fran')
 
88
[Sears]
 
89
 
 
90
"""