-
Notifications
You must be signed in to change notification settings - Fork 770
/
Copy pathcompat.py
62 lines (43 loc) · 1.46 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
from __future__ import absolute_import
import django
from django.conf import settings
from django.utils.timezone import make_aware as make_aware_orig
# django-crispy-forms is optional
try:
import crispy_forms
except ImportError:
crispy_forms = None
def is_crispy():
return 'crispy_forms' in settings.INSTALLED_APPS and crispy_forms
# coreapi is optional (Note that uritemplate is a dependency of coreapi)
# Fixes #525 - cannot simply import from rest_framework.compat, due to
# import issues w/ django-guardian.
try:
import coreapi
except ImportError:
coreapi = None
def remote_field(field):
"""
https://docs.djangoproject.com/en/1.9/releases/1.9/#field-rel-changes
"""
if django.VERSION >= (1, 9):
return field.remote_field
return field.rel
def remote_model(field):
if django.VERSION >= (1, 9):
return remote_field(field).model
return remote_field(field).to
def remote_queryset(field):
model = remote_model(field)
limit_choices_to = field.get_limit_choices_to()
return model._default_manager.complex_filter(limit_choices_to)
def format_value(widget, value):
if django.VERSION >= (1, 10):
return widget.format_value(value)
return widget._format_value(value)
def make_aware(value, timezone, is_dst):
"""is_dst was added for 1.9"""
if django.VERSION >= (1, 9):
return make_aware_orig(value, timezone, is_dst)
else:
return make_aware_orig(value, timezone)