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')
|
|
|
|
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 12:11:04 +00:00
|
|
|
def __unicode__(self):
|
|
|
|
return u"%s %s" % (self.first_name, self.last_name)
|
2006-05-02 01:31:56 +00:00
|
|
|
|
|
|
|
class Business(models.Model):
|
|
|
|
name = models.CharField(maxlength=20, primary_key=True)
|
|
|
|
employees = models.ManyToManyField(Employee)
|
|
|
|
class Meta:
|
|
|
|
verbose_name_plural = 'businesses'
|
|
|
|
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 12:11:04 +00:00
|
|
|
def __unicode__(self):
|
2006-05-02 01:31:56 +00:00
|
|
|
return self.name
|
|
|
|
|
2006-08-27 13:59:47 +00:00
|
|
|
__test__ = {'API_TESTS':"""
|
2006-05-02 01:31:56 +00:00
|
|
|
>>> 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
|
|
|
|
2006-09-25 13:49:01 +00:00
|
|
|
# pk can be used as a substitute for the primary key.
|
|
|
|
>>> Employee.objects.filter(pk__in=['ABC123','XYZ456'])
|
|
|
|
[<Employee: Fran Bones>, <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'])
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 12:11:04 +00:00
|
|
|
{u'XYZ456': <Employee: Fran Jones>, u'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'])
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 12:11:04 +00:00
|
|
|
{u'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
|
|
|
|
2006-08-27 13:59:47 +00:00
|
|
|
"""}
|