/
compat.py
65 lines (48 loc) · 1.5 KB
/
compat.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import django
def get_GenericForeignKey():
try:
from django.contrib.contenttypes.fields import GenericForeignKey
# For Django 1.6 and earlier
except ImportError:
from django.contrib.contenttypes.generic import GenericForeignKey
return GenericForeignKey
def get_GenericRelation():
try:
from django.contrib.contenttypes.fields import GenericRelation
# For Django 1.6 and earlier
except ImportError:
from django.contrib.contenttypes.generic import GenericRelation
return GenericRelation
try:
from collections import OrderedDict
except ImportError:
from django.utils.datastructures import SortedDict as OrderedDict
try:
import importlib
except ImportError:
from django.utils import importlib
try:
from django.db.transaction import atomic
# For django 1.5 and earlier
except ImportError:
from django.db.transaction import commit_on_success as atomic
try:
from django.apps import apps
get_model = apps.get_model
except ImportError:
from django.db.models import get_model
def get_field(model, field_name):
if django.VERSION < (1, 8):
return model._meta.get_field_by_name(field_name)[0]
else:
return model._meta.get_field(field_name)
def get_remote_field(field):
if django.VERSION < (1, 9):
return field.rel
else:
return field.remote_field
def get_remote_field_to(field):
if django.VERSION < (1, 9):
return field.rel.to
else:
return field.remote_field.model