|
1 |
| -""" |
2 |
| -Utils. |
3 |
| -""" |
4 | 1 | import copy
|
5 | 2 | import inspect
|
6 | 3 | import operator
|
7 | 4 | import warnings
|
8 | 5 | from collections import OrderedDict
|
9 | 6 |
|
10 |
| -import django |
11 | 7 | import inflection
|
12 | 8 | from django.conf import settings
|
13 | 9 | from django.db.models import Manager
|
| 10 | +from django.db.models.fields.related_descriptors import ( |
| 11 | + ManyToManyDescriptor, |
| 12 | + ReverseManyToOneDescriptor |
| 13 | +) |
14 | 14 | from django.utils import encoding, six
|
15 | 15 | from django.utils.module_loading import import_string as import_class_from_dotted_path
|
16 | 16 | from django.utils.translation import ugettext_lazy as _
|
17 | 17 | from rest_framework import exceptions
|
18 | 18 | from rest_framework.exceptions import APIException
|
19 |
| - |
20 |
| -try: |
21 |
| - from rest_framework.serializers import ManyRelatedField |
22 |
| -except ImportError: |
23 |
| - ManyRelatedField = object() |
| 19 | +from rest_framework.serializers import ManyRelatedField # noqa: F401 |
24 | 20 |
|
25 | 21 | try:
|
26 | 22 | from rest_framework_nested.relations import HyperlinkedRouterField
|
27 | 23 | except ImportError:
|
28 | 24 | HyperlinkedRouterField = object()
|
29 | 25 |
|
30 |
| -if django.VERSION >= (1, 9): |
31 |
| - from django.db.models.fields.related_descriptors import ( |
32 |
| - ManyToManyDescriptor, ReverseManyToOneDescriptor # noqa: F401 |
33 |
| - ) |
34 |
| - ReverseManyRelatedObjectsDescriptor = object() |
35 |
| -else: |
36 |
| - from django.db.models.fields.related import ( # noqa: F401 |
37 |
| - ManyRelatedObjectsDescriptor as ManyToManyDescriptor |
38 |
| - ) |
39 |
| - from django.db.models.fields.related import ( |
40 |
| - ForeignRelatedObjectsDescriptor as ReverseManyToOneDescriptor |
41 |
| - ) |
42 |
| - from django.db.models.fields.related import ReverseManyRelatedObjectsDescriptor # noqa: F401 |
43 |
| - |
44 | 26 | # Generic relation descriptor from django.contrib.contenttypes.
|
45 | 27 | if 'django.contrib.contenttypes' not in settings.INSTALLED_APPS: # pragma: no cover
|
46 | 28 | # Target application does not use contenttypes. Importing would cause errors.
|
47 | 29 | ReverseGenericManyToOneDescriptor = object()
|
48 |
| -elif django.VERSION >= (1, 9): |
49 |
| - from django.contrib.contenttypes.fields import ReverseGenericManyToOneDescriptor # noqa: F401 |
50 | 30 | else:
|
51 |
| - from django.contrib.contenttypes.fields import ( # noqa: F401 |
52 |
| - ReverseGenericRelatedObjectsDescriptor as ReverseGenericManyToOneDescriptor # noqa: F401 |
53 |
| - ) |
| 31 | + from django.contrib.contenttypes.fields import ReverseGenericManyToOneDescriptor |
54 | 32 |
|
55 | 33 |
|
56 | 34 | def get_resource_name(context, expand_polymorphic_types=False):
|
@@ -236,30 +214,14 @@ def get_related_resource_type(relation):
|
236 | 214 |
|
237 | 215 | parent_model_relation_type = type(parent_model_relation)
|
238 | 216 | if parent_model_relation_type is ReverseManyToOneDescriptor:
|
239 |
| - if django.VERSION >= (1, 9): |
240 |
| - relation_model = parent_model_relation.rel.related_model |
241 |
| - elif django.VERSION >= (1, 8): |
242 |
| - relation_model = parent_model_relation.related.related_model |
243 |
| - else: |
244 |
| - relation_model = parent_model_relation.related.model |
| 217 | + relation_model = parent_model_relation.rel.related_model |
245 | 218 | elif parent_model_relation_type is ManyToManyDescriptor:
|
246 |
| - if django.VERSION >= (1, 9): |
247 |
| - relation_model = parent_model_relation.field.remote_field.model |
248 |
| - # In case we are in a reverse relation |
249 |
| - if relation_model == parent_model: |
250 |
| - relation_model = parent_model_relation.field.model |
251 |
| - elif django.VERSION >= (1, 8): |
252 |
| - relation_model = parent_model_relation.related.model |
253 |
| - # In case we are in a reverse relation |
254 |
| - if relation_model == parent_model: |
255 |
| - relation_model = parent_model_relation.related.related_model |
256 |
| - elif parent_model_relation_type is ReverseManyRelatedObjectsDescriptor: |
257 |
| - relation_model = parent_model_relation.field.related.model |
| 219 | + relation_model = parent_model_relation.field.remote_field.model |
| 220 | + # In case we are in a reverse relation |
| 221 | + if relation_model == parent_model: |
| 222 | + relation_model = parent_model_relation.field.model |
258 | 223 | elif parent_model_relation_type is ReverseGenericManyToOneDescriptor:
|
259 |
| - if django.VERSION >= (1, 9): |
260 |
| - relation_model = parent_model_relation.rel.model |
261 |
| - else: |
262 |
| - relation_model = parent_model_relation.field.related_model |
| 224 | + relation_model = parent_model_relation.rel.model |
263 | 225 | elif hasattr(parent_model_relation, 'field'):
|
264 | 226 | try:
|
265 | 227 | relation_model = parent_model_relation.field.remote_field.model
|
|
0 commit comments