-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use get_fields() instead of unoffical APIs
Both get_all_related_m2m_objects_with_model and get_all_related_objects are scheduled for removal in Django 1.10 and their use generates warnings, e.g. RemovedInDjango110Warning: 'get_all_related_objects is an unofficial API that has been deprecated. You may be able to replace it with 'get_fields()' The Django docs have drop-in replacement code using get fields: https://docs.djangoproject.com/en/1.9/ref/models/meta/ This introduces a compatibility layer for these APIs, Django versions prior to 1.8 will pass through to the original methods and 1.8 and 1.9 will use the replacement code based on get_fields as given in the Django docs.
- Loading branch information
1 parent
80e9e19
commit a875cde
Showing
2 changed files
with
24 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,28 @@ | ||
import django | ||
|
||
if django.VERSION < (1, 8): | ||
def get_all_related_objects(obj): | ||
return obj._meta.get_all_related_objects() | ||
|
||
def get_all_related_m2m_objects_with_model(obj): | ||
return obj._meta.get_all_related_m2m_objects_with_model() | ||
|
||
def get_compat_local_fields(obj): | ||
# virtual_fields are used to collect GenericForeignKey objects | ||
return obj._meta.local_fields + obj._meta.virtual_fields | ||
else: | ||
def get_all_related_objects(obj): | ||
return [ | ||
f for f in obj._meta.get_fields() | ||
if (f.one_to_many or f.one_to_one) and f.auto_created | ||
] | ||
|
||
def get_all_related_m2m_objects_with_model(obj): | ||
return [ | ||
(f, f.model if f.model != obj.__class__ else None) | ||
for f in obj._meta.get_fields(include_hidden=True) | ||
if f.many_to_many and f.auto_created | ||
] | ||
|
||
def get_compat_local_fields(obj): | ||
return obj._meta.get_fields() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters