Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #8669 -- Use a consistent version of create() across the board for

model/field instance creation. Based on a patch from Richard Davies.


git-svn-id: http://code.djangoproject.com/svn/django/trunk@8884 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 85ebb91846fd5bb1a766d1bed905d6f60667274c 1 parent 0e5faf2
Malcolm Tredinnick authored September 03, 2008
4  django/contrib/contenttypes/generic.py
@@ -271,9 +271,7 @@ def clear(self):
271 271
         def create(self, **kwargs):
272 272
             kwargs[self.content_type_field_name] = self.content_type
273 273
             kwargs[self.object_id_field_name] = self.pk_val
274  
-            obj = self.model(**kwargs)
275  
-            obj.save()
276  
-            return obj
  274
+            return super(GenericRelatedObjectManager, self).create(**kwargs)
277 275
         create.alters_data = True
278 276
 
279 277
     return GenericRelatedObjectManager
8  django/db/models/fields/related.py
@@ -306,9 +306,8 @@ def add(self, *objs):
@@ -410,8 +409,7 @@ def create(self, **kwargs):
77  tests/modeltests/custom_pk/models.py
@@ -6,11 +6,11 @@
6 6
 this behavior by explicitly adding ``primary_key=True`` to a field.
7 7
 """
8 8
 
9  
-from django.db import models
  9
+from django.conf import settings
  10
+from django.db import models, transaction, IntegrityError
10 11
 
11 12
 class Employee(models.Model):
12  
-    employee_code = models.CharField(max_length=10, primary_key=True,
13  
-            db_column = 'code')
  13
+    employee_code = models.IntegerField(primary_key=True, db_column = 'code')
14 14
     first_name = models.CharField(max_length=20)
15 15
     last_name = models.CharField(max_length=20)
16 16
     class Meta:
@@ -29,50 +29,50 @@ def __unicode__(self):
29 29
         return self.name
30 30
 
31 31
 __test__ = {'API_TESTS':"""
32  
->>> dan = Employee(employee_code='ABC123', first_name='Dan', last_name='Jones')
  32
+>>> dan = Employee(employee_code=123, first_name='Dan', last_name='Jones')
33 33
 >>> dan.save()
34 34
 >>> Employee.objects.all()
35 35
 [<Employee: Dan Jones>]
36 36
 
37  
->>> fran = Employee(employee_code='XYZ456', first_name='Fran', last_name='Bones')
  37
+>>> fran = Employee(employee_code=456, first_name='Fran', last_name='Bones')
38 38
 >>> fran.save()
39 39
 >>> Employee.objects.all()
40 40
 [<Employee: Fran Bones>, <Employee: Dan Jones>]
41 41
 
42  
->>> Employee.objects.get(pk='ABC123')
  42
+>>> Employee.objects.get(pk=123)
43 43
 <Employee: Dan Jones>
44  
->>> Employee.objects.get(pk='XYZ456')
  44
+>>> Employee.objects.get(pk=456)
45 45
 <Employee: Fran Bones>
46  
->>> Employee.objects.get(pk='foo')
  46
+>>> Employee.objects.get(pk=42)
47 47
 Traceback (most recent call last):
48 48
     ...
49 49
 DoesNotExist: Employee matching query does not exist.
50 50
 
51 51
 # Use the name of the primary key, rather than pk.
52  
->>> Employee.objects.get(employee_code__exact='ABC123')
  52
+>>> Employee.objects.get(employee_code__exact=123)
53 53
 <Employee: Dan Jones>
54 54
 
55 55
 # pk can be used as a substitute for the primary key.
56  
->>> Employee.objects.filter(pk__in=['ABC123','XYZ456'])
  56
+>>> Employee.objects.filter(pk__in=[123, 456])
57 57
 [<Employee: Fran Bones>, <Employee: Dan Jones>]
58 58
 
59 59
 # The primary key can be accessed via the pk property on the model.
60  
->>> e = Employee.objects.get(pk='ABC123')
  60
+>>> e = Employee.objects.get(pk=123)
61 61
 >>> e.pk
62  
-u'ABC123'
  62
+123
63 63
 
64 64
 # Or we can use the real attribute name for the primary key:
65 65
 >>> e.employee_code
66  
-u'ABC123'
  66
+123
67 67
 
68 68
 # Fran got married and changed her last name.
69  
->>> fran = Employee.objects.get(pk='XYZ456')
  69
+>>> fran = Employee.objects.get(pk=456)
70 70
 >>> fran.last_name = 'Jones'
71 71
 >>> fran.save()
72 72
 >>> Employee.objects.filter(last_name__exact='Jones')
73 73
 [<Employee: Dan Jones>, <Employee: Fran Jones>]
74  
->>> emps = Employee.objects.in_bulk(['ABC123', 'XYZ456'])
75  
->>> emps['ABC123']
  74
+>>> emps = Employee.objects.in_bulk([123, 456])
  75
+>>> emps[123]
76 76
 <Employee: Dan Jones>
77 77
 
78 78
 >>> b = Business(name='Sears')
@@ -96,15 +96,52 @@ def __unicode__(self):
96 96
 >>> Employee.objects.filter(business__pk='Sears')
97 97
 [<Employee: Dan Jones>, <Employee: Fran Jones>]
98 98
 
99  
->>> Business.objects.filter(employees__employee_code__exact='ABC123')
  99
+>>> Business.objects.filter(employees__employee_code__exact=123)
100 100
 [<Business: Sears>]
101  
->>> Business.objects.filter(employees__pk='ABC123')
  101
+>>> Business.objects.filter(employees__pk=123)
102 102
 [<Business: Sears>]
103 103
 >>> Business.objects.filter(employees__first_name__startswith='Fran')
104 104
 [<Business: Sears>]
105 105
 
106 106
 # Primary key may be unicode string
107  
->>> emp = Employee(employee_code='jaźń')
108  
->>> emp.save()
  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
109 123
 
110 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
+"""
19  tests/modeltests/get_or_create/models.py
@@ -16,6 +16,10 @@ class Person(models.Model):
16 16
     def __unicode__(self):
17 17
         return u'%s %s' % (self.first_name, self.last_name)
18 18
 
  19
+class ManualPrimaryKeyTest(models.Model):
  20
+    id = models.IntegerField(primary_key=True)
  21
+    data = models.CharField(max_length=100)
  22
+
19 23
 __test__ = {'API_TESTS':"""
20 24
 # Acting as a divine being, create an Person.
21 25
 >>> from datetime import date
@@ -61,4 +65,19 @@ def __unicode__(self):
61 65
 ...     else:
62 66
 ...         print "Fail with %s" % type(e)
63 67
 Pass
  68
+
  69
+# If you specify an existing primary key, but different other fields, then you
  70
+# will get an error and data will not be updated.
  71
+>>> m = ManualPrimaryKeyTest(id=1, data='Original')
  72
+>>> m.save()
  73
+>>> try:
  74
+...    m, created = ManualPrimaryKeyTest.objects.get_or_create(id=1, data='Different')
  75
+... except Exception, e:
  76
+...    if isinstance(e, IntegrityError):
  77
+...        print "Pass"
  78
+...    else:
  79
+...        print "Fail with %s" % type(e)
  80
+Pass
  81
+>>> ManualPrimaryKeyTest.objects.get(id=1).data == 'Original'
  82
+True
64 83
 """}

0 notes on commit 85ebb91

Please sign in to comment.
Something went wrong with that request. Please try again.