Code

Ticket #7938: 7983_auto_field.diff

File 7983_auto_field.diff, 7.6 KB (added by magneto, 6 years ago)

(bad diff file)

Line 
1Index: core/management/sql.py
2===================================================================
3--- core/management/sql.py      (revision 8138)
4+++ core/management/sql.py      (working copy)
5@@ -54,7 +54,7 @@
6     for app in apps:
7         for model in models.get_models(app):
8             for f in model._meta.local_fields:
9-                if isinstance(f, models.AutoField):
10+                if isinstance(f, models.AutoField) or getattr(f, 'auto_field', False):
11                     sequence_list.append({'table': model._meta.db_table, 'column': f.column})
12                     break # Only one AutoField is allowed per model, so don't bother continuing.
13Index: db/models/base.py
14===================================================================
15--- db/models/base.py   (revision 8138)
16+++ db/models/base.py   (working copy)
17@@ -140,7 +140,7 @@
18             value.contribute_to_class(cls, name)
19         else:
20             setattr(cls, name, value)
21-
22+
23     def _prepare(cls):
24         """
25         Creates some methods once self._meta has been populated.
26@@ -327,7 +327,7 @@
27                 record_exists = False
28         if not pk_set or not record_exists:
29             if not pk_set:
30-                values = [(f, f.get_db_prep_save(raw and getattr(self, f.attname) or f.pre_save(self, True))) for f in meta.local_fields if not isinstance(f, AutoField)]
31+                values = [(f, f.get_db_prep_save(raw and getattr(self, f.attname) or f.pre_save(self, True))) for f in meta.local_fields if not isinstance(f, AutoField) and not getattr(f, 'auto_field', False)]
32             else:
33                 values = [(f, f.get_db_prep_save(raw and getattr(self, f.attname) or f.pre_save(self, True))) for f in meta.local_fields]
34Index: db/models/manipulators.py
35===================================================================
36--- db/models/manipulators.py   (revision 8138)
37+++ db/models/manipulators.py   (working copy)
38@@ -179,7 +179,7 @@
39 
40                         if f == related.field:
41                             param = getattr(new_object, related.field.rel.get_related_field().attname)
42-                        elif (not self.change) and isinstance(f, AutoField):
43+                        elif (not self.change) and (isinstance(f, AutoField) or getattr(f, 'auto_field', False)):
44                             param = None
45                         elif self.change and (isinstance(f, FileField) or not child_follow.get(f.name, None)):
46                             if old_rel_obj:
47Index: db/models/fields/__init__.py
48===================================================================
49--- db/models/fields/__init__.py        (revision 8138)
50+++ db/models/fields/__init__.py        (working copy)
51@@ -82,7 +82,7 @@
52             editable=True, serialize=True, unique_for_date=None,
53             unique_for_month=None, unique_for_year=None, validator_list=None,
54             choices=None, help_text='', db_column=None, db_tablespace=None,
55-            auto_created=False):
56+            auto_created=False, auto_field=False):
57         self.name = name
58         self.verbose_name = verbose_name
59         self.primary_key = primary_key
60@@ -101,6 +101,7 @@
61         self._choices = choices or []
62         self.help_text = help_text
63         self.db_column = db_column
64+        self.auto_field = auto_field
65         self.db_tablespace = db_tablespace or settings.DEFAULT_INDEX_TABLESPACE
66 
67         # Set db_index to True if the field has a relationship and doesn't explicitly set db_index.
68@@ -199,6 +200,10 @@
69         cls._meta.add_field(self)
70         if self.choices:
71             setattr(cls, 'get_%s_display' % self.name, curry(cls._get_FIELD_display, field=self))
72+       if self.auto_field:
73+                       assert not cls._meta.has_auto_field, "A model can't have more than one auto_field=True or AutoFields."
74+                       cls._meta.has_auto_field = True
75+                       cls._meta.auto_field = self
76 
77     def get_attname(self):
78         return self.name
79@@ -337,7 +342,7 @@
80         # If this field is in a related context, check whether any other fields
81         # in the related object have core=True. If so, add a validator --
82         # RequiredIfOtherFieldsGiven -- to this FormField.
83-        if rel and not self.blank and not isinstance(self, AutoField) and not isinstance(self, FileField):
84+        if rel and not self.blank and not isinstance(self, FileField) and not self.auto_field:
85             # First, get the core fields, if any.
86             core_field_names = []
87             for f in opts.fields:
88@@ -434,6 +439,8 @@
89 
90     def formfield(self, form_class=forms.CharField, **kwargs):
91         "Returns a django.forms.Field instance for this database Field."
92+        if self.auto_field:
93+               return None
94         defaults = {'required': not self.blank, 'label': capfirst(self.verbose_name), 'help_text': self.help_text}
95         if self.choices:
96             defaults['widget'] = forms.Select(choices=self.get_choices(include_blank=self.blank or not (self.has_default() or 'initial' in kwargs)))
97@@ -451,6 +458,7 @@
98     def __init__(self, *args, **kwargs):
99         assert kwargs.get('primary_key', False) is True, "%ss must have primary_key=True." % self.__class__.__name__
100         kwargs['blank'] = True
101+        kwargs['auto_field'] = True
102         Field.__init__(self, *args, **kwargs)
103 
104     def to_python(self, value):
105@@ -482,12 +490,6 @@
106             return None
107         return Field.get_manipulator_new_data(self, new_data, rel)
108 
109-    def contribute_to_class(self, cls, name):
110-        assert not cls._meta.has_auto_field, "A model can't have more than one AutoField."
111-        super(AutoField, self).contribute_to_class(cls, name)
112-        cls._meta.has_auto_field = True
113-        cls._meta.auto_field = self
114-
115     def formfield(self, **kwargs):
116         return None
117Index: forms/models.py
118===================================================================
119--- forms/models.py     (revision 8138)
120+++ forms/models.py     (working copy)
121@@ -37,7 +37,7 @@
122                          " validate." % (opts.object_name, fail_message))
123     cleaned_data = form.cleaned_data
124     for f in opts.fields:
125-        if not f.editable or isinstance(f, models.AutoField) \
126+        if not f.editable or isinstance(f, models.AutoField) or f.auto_field \
127                 or not f.name in cleaned_data:
128             continue
129         if fields and f.name not in fields:
130@@ -378,8 +378,9 @@
131 
132     def add_fields(self, form, index):
133         """Add a hidden field for the object's primary key."""
134-        self._pk_field_name = self.model._meta.pk.attname
135-        form.fields[self._pk_field_name] = IntegerField(required=False, widget=HiddenInput)
136+        if self.model._meta.has_auto_field:
137+            self._pk_field_name = self.model._meta.pk.attname
138+            form.fields[self._pk_field_name] = IntegerField(required=False, widget=HiddenInput)
139         super(BaseModelFormSet, self).add_fields(form, index)
140 
141 def modelformset_factory(model, form=ModelForm, formfield_callback=lambda f: f.formfield(),
142Index: core/management/sql.py
143===================================================================
144--- core/management/sql.py      (revision 8138)
145+++ core/management/sql.py      (working copy)
146@@ -54,7 +54,7 @@
147     for app in apps:
148         for model in models.get_models(app):
149             for f in model._meta.local_fields:
150-                if isinstance(f, models.AutoField):
151+                if isinstance(f, models.AutoField) or getattr(f, 'auto_field', False):
152                     sequence_list.append({'table': model._meta.db_table, 'column': f.column})
153                     break # Only one AutoField is allowed per model, so don't bother continuing.