Skip to content
This repository was archived by the owner on Apr 1, 2021. It is now read-only.

Add model field meta API compatibility #6

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
124 changes: 124 additions & 0 deletions django_seven/compat/models/meta.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
from itertools import chain

import django

"""
All these functions have been copied from Django documentation
https://docs.djangoproject.com/en/1.10/ref/models/meta/
"""


def get_field(model_class, name):
if django.VERSION < (1, 9):
return model_class._meta.get_field(name)
else:
f = model_class._meta.get_field(name)
if f.auto_created is False and f.is_relation and f.related_model is None:
return f


def get_field_by_name(model_class, name):
if django.VERSION < (1, 9):
return model_class._meta.get_field_by_name(name)
else:
field = model_class._meta.get_field(name)
model = field.model
direct = not field.auto_created or field.concrete
m2m = field.many_to_many
return field, model, direct, m2m


def get_fields_with_model(model_class):
if django.VERSION < (1, 9):
return model_class._meta.get_fields_with_model()
else:
return [
(f, f.model if f.model != model_class else None)
for f in model_class._meta.get_fields()
if not f.is_relation
or f.one_to_one
or (f.many_to_one and f.related_model)
]


def get_concrete_fields_with_model(model_class):
if django.VERSION < (1, 9):
return model_class._meta.get_concrete_fields_with_model()
else:
return [
(f, f.model if f.model != model_class else None)
for f in model_class._meta.get_fields()
if f.concrete and (
not f.is_relation
or f.one_to_one
or (f.many_to_one and f.related_model)
)
]


def get_m2m_with_model(model_class):
if django.VERSION < (1, 9):
return model_class._meta.get_m2m_with_model()
else:
return [
(f, f.model if f.model != model_class else None)
for f in model_class._meta.get_fields()
if f.many_to_many and not f.auto_created
]


def get_all_related_objects(model_class):
if django.VERSION < (1, 9):
return model_class._meta.get_all_related_objects()
else:
return [
f for f in model_class._meta.get_fields()
if (f.one_to_many or f.one_to_one)
and f.auto_created and not f.concrete
]


def get_all_related_objects_with_model(model_class):
if django.VERSION < (1, 9):
return model_class._meta.get_all_related_objects_with_model()
else:
return [
(f, f.model if f.model != model_class else None)
for f in model_class._meta.get_fields()
if (f.one_to_many or f.one_to_one)
and f.auto_created and not f.concrete
]


def get_all_related_many_to_many_objects(model_class):
if django.VERSION < (1, 9):
return model_class._meta.get_all_related_many_to_many_objects()
else:
return [
f for f in model_class._meta.get_fields(include_hidden=True)
if f.many_to_many and f.auto_created
]


def get_all_related_m2m_objects_with_model(model_class):
if django.VERSION < (1, 9):
return model_class._meta.get_all_related_m2m_objects_with_model()
else:
return [
(f, f.model if f.model != model_class else None)
for f in model_class._meta.get_fields(include_hidden=True)
if f.many_to_many and f.auto_created
]


def get_all_field_names(model_class):
if django.VERSION < (1, 9):
return model_class._meta.get_all_field_names()
else:
return list(set(chain.from_iterable(
(field.name, field.attname) if hasattr(field, 'attname') else (field.name,)
for field in model_class._meta.get_fields()
# For complete backwards compatibility, you may want to exclude
# GenericForeignKey from the results.
if not (field.many_to_one and field.related_model is None)
)))
1 change: 0 additions & 1 deletion tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
__author__ = 'romgar'
31 changes: 31 additions & 0 deletions tests/compat/test_models_meta.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from django.test import TestCase

from django_seven.compat.models import meta as compat_meta
from tests.models import SuperModel


class TestModelMetaMethods(TestCase):

def test_get_fields_with_model(self):
self.assertEqual(compat_meta.get_fields_with_model(SuperModel), [])

def get_concrete_fields_with_model(self):
self.assertEqual(compat_meta.get_concrete_fields_with_model(SuperModel), [])

def get_m2m_with_model(self):
self.assertEqual(compat_meta.get_m2m_with_model(SuperModel), [])

def get_all_related_objects(self):
self.assertEqual(compat_meta.get_all_related_objects(SuperModel), [])

def get_all_related_objects_with_model(self):
self.assertEqual(compat_meta.get_all_related_objects_with_model(SuperModel), [])

def get_all_related_many_to_many_objects(self):
self.assertEqual(compat_meta.get_all_related_many_to_many_objects(SuperModel), [])

def get_all_related_m2m_objects_with_model(self):
self.assertEqual(compat_meta.get_all_related_m2m_objects_with_model(SuperModel), [])

def get_all_field_names(self):
self.assertEqual(compat_meta.get_all_field_names(SuperModel), [])
37 changes: 37 additions & 0 deletions tests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,40 @@

class SuperHero(models.Model):
name = models.CharField(max_length=255)


class SuperForeignKeyModel(models.Model):
name = models.CharField(max_length=255)

class Meta:
managed = False


class SuperM2MModel(models.Model):
name = models.CharField(max_length=255)

class Meta:
managed = False


class SuperModel(models.Model):
non_related_field = models.CharField(max_length=255)
foreign_key_field = models.ManyToManyField(SuperForeignKeyModel)
m2m_field = models.ManyToManyField(SuperM2MModel)

class Meta:
managed = False


class SuperRelatedToSuperModel(models.Model):
foreign_key_field = models.ForeignKey(SuperModel)

class Meta:
managed = False


class SuperRelated2ToSuperModel(models.Model):
m2m_field = models.ManyToManyField(SuperModel)

class Meta:
managed = False