Code

Ticket #8576: multiple_autofields.diff

File multiple_autofields.diff, 1.4 KB (added by honeyman, 6 years ago)

Multiple AutoField-s

Line 
1Index: django/db/models/fields/__init__.py
2===================================================================
3--- django/db/models/fields/__init__.py (revision 8599)
4+++ django/db/models/fields/__init__.py (working copy)
5@@ -422,7 +422,6 @@
6 class AutoField(Field):
7     empty_strings_allowed = False
8     def __init__(self, *args, **kwargs):
9-        assert kwargs.get('primary_key', False) is True, "%ss must have primary_key=True." % self.__class__.__name__
10         kwargs['blank'] = True
11         Field.__init__(self, *args, **kwargs)
12 
13@@ -456,7 +455,6 @@
14         return Field.get_manipulator_new_data(self, new_data, rel)
15 
16     def contribute_to_class(self, cls, name):
17-        assert not cls._meta.has_auto_field, "A model can't have more than one AutoField."
18         super(AutoField, self).contribute_to_class(cls, name)
19         cls._meta.has_auto_field = True
20         cls._meta.auto_field = self
21Index: tests/regressiontests/model_fields/models.py
22===================================================================
23--- tests/regressiontests/model_fields/models.py        (revision 8599)
24+++ tests/regressiontests/model_fields/models.py        (working copy)
25@@ -78,5 +78,11 @@
26 >>> Foo.objects.filter(d=u'1.23')
27 []
28 
29+# Make sure it is possible to create multiple AutoFields in a model,
30+# while neither of them is a primary key.
31+>>> class MyClass(models.Model):
32+...    f1 = models.AutoField()
33+...    f2 = models.AutoField()
34 
35+
36 """}