|
10 | 10 | from django.db.models import loading
|
11 | 11 | from django.db.models.fields.proxy import OrderWrt
|
12 | 12 | from django.db.models.fields.related import RelatedField
|
13 |
| -from django.db.models.related import RelatedObject |
| 13 | +try: |
| 14 | + from django.db.models.fields.related import RelatedObject |
| 15 | +except ImportError: |
| 16 | + pass |
14 | 17 | from django.conf import settings
|
15 | 18 | from django.contrib import admin
|
16 | 19 | from django.utils import importlib, six
|
@@ -121,18 +124,22 @@ def copy_fields(self, model):
|
121 | 124 | for field in model._meta.fields:
|
122 | 125 | field = copy.copy(field)
|
123 | 126 | field.rel = copy.copy(field.rel)
|
| 127 | + if isinstance(field, OrderWrt): |
| 128 | + # OrderWrt is a proxy field, switch to a plain IntegerField |
| 129 | + field.__class__ = models.IntegerField |
124 | 130 | if isinstance(field, models.ForeignKey):
|
125 | 131 | # Don't allow reverse relations.
|
126 | 132 | # ForeignKey knows best what datatype to use for the column
|
127 | 133 | # we'll used that as soon as it's finalized by copying rel.to
|
128 |
| - field.__class__ = CustomForeignKeyField |
| 134 | + old_field = field |
| 135 | + field = type(field)(field.rel.to, related_name='+', null=True, blank=True) |
| 136 | + field.rel = old_field.rel |
129 | 137 | field.rel.related_name = '+'
|
130 |
| - field.null = True |
131 |
| - field.blank = True |
132 |
| - if isinstance(field, OrderWrt): |
133 |
| - # OrderWrt is a proxy field, switch to a plain IntegerField |
134 |
| - field.__class__ = models.IntegerField |
135 |
| - transform_field(field) |
| 138 | + field.name = old_field.name |
| 139 | + field.db_constraint = False |
| 140 | + field._unique = False |
| 141 | + else: |
| 142 | + transform_field(field) |
136 | 143 | fields[field.name] = field
|
137 | 144 | return fields
|
138 | 145 |
|
|
0 commit comments