2007-09-15 02:36:04 +08:00
|
|
|
# -*- coding: utf-8 -*-
|
2006-05-02 09:31:56 +08: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.
|
|
|
|
"""
|
|
|
|
|
2008-09-03 08:09:33 +08:00
|
|
|
from django.conf import settings
|
|
|
|
from django.db import models, transaction, IntegrityError
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2009-06-08 12:50:24 +08:00
|
|
|
from fields import MyAutoField
|
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
class Employee(models.Model):
|
2008-09-03 08:09:33 +08:00
|
|
|
employee_code = models.IntegerField(primary_key=True, db_column = 'code')
|
2007-08-05 13:14:46 +08:00
|
|
|
first_name = models.CharField(max_length=20)
|
|
|
|
last_name = models.CharField(max_length=20)
|
2006-05-02 09:31:56 +08:00
|
|
|
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 20:11:04 +08:00
|
|
|
def __unicode__(self):
|
|
|
|
return u"%s %s" % (self.first_name, self.last_name)
|
2006-05-02 09:31:56 +08:00
|
|
|
|
|
|
|
class Business(models.Model):
|
2007-08-05 13:14:46 +08:00
|
|
|
name = models.CharField(max_length=20, primary_key=True)
|
2006-05-02 09:31:56 +08:00
|
|
|
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 20:11:04 +08:00
|
|
|
def __unicode__(self):
|
2006-05-02 09:31:56 +08:00
|
|
|
return self.name
|
|
|
|
|
2009-06-08 12:50:24 +08:00
|
|
|
class Bar(models.Model):
|
|
|
|
id = MyAutoField(primary_key=True, db_index=True)
|
|
|
|
|
|
|
|
def __unicode__(self):
|
|
|
|
return repr(self.pk)
|
|
|
|
|
|
|
|
|
|
|
|
class Foo(models.Model):
|
|
|
|
bar = models.ForeignKey(Bar)
|
|
|
|
|
2006-08-27 21:59:47 +08:00
|
|
|
__test__ = {'API_TESTS':"""
|
2008-09-03 08:09:33 +08:00
|
|
|
>>> dan = Employee(employee_code=123, first_name='Dan', last_name='Jones')
|
2006-05-02 09:31:56 +08:00
|
|
|
>>> dan.save()
|
|
|
|
>>> Employee.objects.all()
|
2006-06-04 08:23:51 +08:00
|
|
|
[<Employee: Dan Jones>]
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2008-09-03 08:09:33 +08:00
|
|
|
>>> fran = Employee(employee_code=456, first_name='Fran', last_name='Bones')
|
2006-05-02 09:31:56 +08:00
|
|
|
>>> fran.save()
|
|
|
|
>>> Employee.objects.all()
|
2006-06-04 08:23:51 +08:00
|
|
|
[<Employee: Fran Bones>, <Employee: Dan Jones>]
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2008-09-03 08:09:33 +08:00
|
|
|
>>> Employee.objects.get(pk=123)
|
2006-06-04 08:23:51 +08:00
|
|
|
<Employee: Dan Jones>
|
2008-09-03 08:09:33 +08:00
|
|
|
>>> Employee.objects.get(pk=456)
|
2006-06-04 08:23:51 +08:00
|
|
|
<Employee: Fran Bones>
|
2008-09-03 08:09:33 +08:00
|
|
|
>>> Employee.objects.get(pk=42)
|
2006-05-02 09:31:56 +08:00
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
2006-05-07 02:36:32 +08:00
|
|
|
DoesNotExist: Employee matching query does not exist.
|
2006-05-02 09:31:56 +08:00
|
|
|
|
|
|
|
# Use the name of the primary key, rather than pk.
|
2008-09-03 08:09:33 +08:00
|
|
|
>>> Employee.objects.get(employee_code__exact=123)
|
2006-06-04 08:23:51 +08:00
|
|
|
<Employee: Dan Jones>
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2006-09-25 21:49:01 +08:00
|
|
|
# pk can be used as a substitute for the primary key.
|
2008-09-03 08:09:33 +08:00
|
|
|
>>> Employee.objects.filter(pk__in=[123, 456])
|
2006-09-25 21:49:01 +08:00
|
|
|
[<Employee: Fran Bones>, <Employee: Dan Jones>]
|
|
|
|
|
2007-09-16 09:57:25 +08:00
|
|
|
# The primary key can be accessed via the pk property on the model.
|
2008-09-03 08:09:33 +08:00
|
|
|
>>> e = Employee.objects.get(pk=123)
|
2007-09-16 09:57:25 +08:00
|
|
|
>>> e.pk
|
2008-09-03 08:09:33 +08:00
|
|
|
123
|
2007-09-16 09:57:25 +08:00
|
|
|
|
|
|
|
# Or we can use the real attribute name for the primary key:
|
|
|
|
>>> e.employee_code
|
2008-09-03 08:09:33 +08:00
|
|
|
123
|
2007-09-16 09:57:25 +08:00
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
# Fran got married and changed her last name.
|
2008-09-03 08:09:33 +08:00
|
|
|
>>> fran = Employee.objects.get(pk=456)
|
2006-05-02 09:31:56 +08:00
|
|
|
>>> fran.last_name = 'Jones'
|
|
|
|
>>> fran.save()
|
|
|
|
>>> Employee.objects.filter(last_name__exact='Jones')
|
2006-06-04 08:23:51 +08:00
|
|
|
[<Employee: Dan Jones>, <Employee: Fran Jones>]
|
2008-09-03 08:09:33 +08:00
|
|
|
>>> emps = Employee.objects.in_bulk([123, 456])
|
|
|
|
>>> emps[123]
|
2008-03-20 03:11:51 +08:00
|
|
|
<Employee: Dan Jones>
|
2006-05-02 09:31:56 +08:00
|
|
|
|
|
|
|
>>> b = Business(name='Sears')
|
|
|
|
>>> b.save()
|
|
|
|
>>> b.employees.add(dan, fran)
|
|
|
|
>>> b.employees.all()
|
2006-06-04 08:23:51 +08:00
|
|
|
[<Employee: Dan Jones>, <Employee: Fran Jones>]
|
2006-05-02 09:31:56 +08:00
|
|
|
>>> fran.business_set.all()
|
2006-06-04 08:23:51 +08:00
|
|
|
[<Business: Sears>]
|
2006-05-02 09:31:56 +08: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 20:11:04 +08:00
|
|
|
{u'Sears': <Business: Sears>}
|
2006-05-02 09:31:56 +08:00
|
|
|
|
|
|
|
>>> Business.objects.filter(name__exact='Sears')
|
2006-06-04 08:23:51 +08:00
|
|
|
[<Business: Sears>]
|
2006-05-02 09:31:56 +08:00
|
|
|
>>> Business.objects.filter(pk='Sears')
|
2006-06-04 08:23:51 +08:00
|
|
|
[<Business: Sears>]
|
2006-05-02 09:31:56 +08:00
|
|
|
|
|
|
|
# Queries across tables, involving primary key
|
|
|
|
>>> Employee.objects.filter(business__name__exact='Sears')
|
2006-06-04 08:23:51 +08:00
|
|
|
[<Employee: Dan Jones>, <Employee: Fran Jones>]
|
2006-05-02 09:31:56 +08:00
|
|
|
>>> Employee.objects.filter(business__pk='Sears')
|
2006-06-04 08:23:51 +08:00
|
|
|
[<Employee: Dan Jones>, <Employee: Fran Jones>]
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2008-09-03 08:09:33 +08:00
|
|
|
>>> Business.objects.filter(employees__employee_code__exact=123)
|
2006-06-04 08:23:51 +08:00
|
|
|
[<Business: Sears>]
|
2008-09-03 08:09:33 +08:00
|
|
|
>>> Business.objects.filter(employees__pk=123)
|
2006-06-04 08:23:51 +08:00
|
|
|
[<Business: Sears>]
|
2006-05-02 09:31:56 +08:00
|
|
|
>>> Business.objects.filter(employees__first_name__startswith='Fran')
|
2006-06-04 08:23:51 +08:00
|
|
|
[<Business: Sears>]
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2007-09-15 02:36:04 +08:00
|
|
|
# Primary key may be unicode string
|
2008-09-03 08:09:33 +08:00
|
|
|
>>> bus = Business(name=u'jaźń')
|
|
|
|
>>> bus.save()
|
|
|
|
|
|
|
|
# The primary key must also obviously be unique, so trying to create a new
|
|
|
|
# object with the same primary key will fail.
|
|
|
|
>>> try:
|
|
|
|
... sid = transaction.savepoint()
|
|
|
|
... Employee.objects.create(employee_code=123, first_name='Fred', last_name='Jones')
|
|
|
|
... transaction.savepoint_commit(sid)
|
|
|
|
... except Exception, e:
|
|
|
|
... if isinstance(e, IntegrityError):
|
|
|
|
... transaction.savepoint_rollback(sid)
|
|
|
|
... print "Pass"
|
|
|
|
... else:
|
|
|
|
... print "Fail with %s" % type(e)
|
|
|
|
Pass
|
2007-09-15 02:36:04 +08:00
|
|
|
|
2009-06-08 12:50:24 +08:00
|
|
|
# Regression for #10785 -- Custom fields can be used for primary keys.
|
|
|
|
>>> new_bar = Bar.objects.create()
|
|
|
|
>>> new_foo = Foo.objects.create(bar=new_bar)
|
2009-06-15 19:47:01 +08:00
|
|
|
|
|
|
|
# FIXME: This still doesn't work, but will require some changes in
|
|
|
|
# get_db_prep_lookup to fix it.
|
|
|
|
# >>> f = Foo.objects.get(bar=new_bar.pk)
|
|
|
|
# >>> f == new_foo
|
|
|
|
# True
|
|
|
|
# >>> f.bar == new_bar
|
|
|
|
# True
|
2009-06-08 12:50:24 +08:00
|
|
|
|
|
|
|
>>> f = Foo.objects.get(bar=new_bar)
|
|
|
|
>>> f == new_foo
|
|
|
|
True
|
|
|
|
>>> f.bar == new_bar
|
|
|
|
True
|
|
|
|
|
2006-08-27 21:59:47 +08:00
|
|
|
"""}
|
2008-09-03 08:09:33 +08:00
|
|
|
|
|
|
|
# SQLite lets objects be saved with an empty primary key, even though an
|
|
|
|
# integer is expected. So we can't check for an error being raised in that case
|
|
|
|
# for SQLite. Remove it from the suite for this next bit.
|
|
|
|
if settings.DATABASE_ENGINE != 'sqlite3':
|
|
|
|
__test__["API_TESTS"] += """
|
|
|
|
# The primary key must be specified, so an error is raised if you try to create
|
|
|
|
# an object without it.
|
|
|
|
>>> try:
|
|
|
|
... sid = transaction.savepoint()
|
|
|
|
... Employee.objects.create(first_name='Tom', last_name='Smith')
|
|
|
|
... print 'hello'
|
|
|
|
... transaction.savepoint_commit(sid)
|
|
|
|
... print 'hello2'
|
|
|
|
... except Exception, e:
|
|
|
|
... if isinstance(e, IntegrityError):
|
|
|
|
... transaction.savepoint_rollback(sid)
|
|
|
|
... print "Pass"
|
|
|
|
... else:
|
|
|
|
... print "Fail with %s" % type(e)
|
|
|
|
Pass
|
|
|
|
|
|
|
|
"""
|