2014-09-09 20:46:28 +04:00
|
|
|
"""
|
2014-09-18 14:20:56 +04:00
|
|
|
Helper function for returning the field information that is associated
|
2014-09-12 12:12:56 +04:00
|
|
|
with a model class. This includes returning all the forward and reverse
|
|
|
|
relationships and their associated metadata.
|
2014-09-18 14:20:56 +04:00
|
|
|
|
|
|
|
Usage: `get_field_info(model)` returns a `FieldInfo` instance.
|
2014-09-09 20:46:28 +04:00
|
|
|
"""
|
2014-09-10 19:57:22 +04:00
|
|
|
from collections import namedtuple
|
2014-11-28 18:36:04 +03:00
|
|
|
from django.core.exceptions import ImproperlyConfigured
|
2014-09-09 20:46:28 +04:00
|
|
|
from django.db import models
|
|
|
|
from django.utils import six
|
2014-11-06 15:00:30 +03:00
|
|
|
from rest_framework.compat import OrderedDict
|
2014-09-09 20:46:28 +04:00
|
|
|
import inspect
|
|
|
|
|
2014-09-18 14:20:56 +04:00
|
|
|
|
|
|
|
FieldInfo = namedtuple('FieldResult', [
|
|
|
|
'pk', # Model field instance
|
|
|
|
'fields', # Dict of field name -> model field instance
|
|
|
|
'forward_relations', # Dict of field name -> RelationInfo
|
|
|
|
'reverse_relations', # Dict of field name -> RelationInfo
|
|
|
|
'fields_and_pk', # Shortcut for 'pk' + 'fields'
|
|
|
|
'relations' # Shortcut for 'forward_relations' + 'reverse_relations'
|
|
|
|
])
|
|
|
|
|
|
|
|
RelationInfo = namedtuple('RelationInfo', [
|
|
|
|
'model_field',
|
|
|
|
'related',
|
|
|
|
'to_many',
|
|
|
|
'has_through_model'
|
|
|
|
])
|
2014-09-09 20:46:28 +04:00
|
|
|
|
|
|
|
|
|
|
|
def _resolve_model(obj):
|
|
|
|
"""
|
|
|
|
Resolve supplied `obj` to a Django model class.
|
|
|
|
|
|
|
|
`obj` must be a Django model class itself, or a string
|
2014-12-29 11:35:00 +03:00
|
|
|
representation of one. Useful in situations like GH #1225 where
|
2014-09-09 20:46:28 +04:00
|
|
|
Django may not have resolved a string-based reference to a model in
|
|
|
|
another model's foreign key definition.
|
|
|
|
|
|
|
|
String representations should have the format:
|
|
|
|
'appname.ModelName'
|
|
|
|
"""
|
|
|
|
if isinstance(obj, six.string_types) and len(obj.split('.')) == 2:
|
|
|
|
app_name, model_name = obj.split('.')
|
2014-11-18 09:26:23 +03:00
|
|
|
resolved_model = models.get_model(app_name, model_name)
|
2014-11-28 18:36:04 +03:00
|
|
|
if resolved_model is None:
|
|
|
|
msg = "Django did not return a model for {0}.{1}"
|
|
|
|
raise ImproperlyConfigured(msg.format(app_name, model_name))
|
2014-11-18 09:26:23 +03:00
|
|
|
return resolved_model
|
2014-09-09 20:46:28 +04:00
|
|
|
elif inspect.isclass(obj) and issubclass(obj, models.Model):
|
|
|
|
return obj
|
|
|
|
raise ValueError("{0} is not a Django model".format(obj))
|
|
|
|
|
|
|
|
|
|
|
|
def get_field_info(model):
|
|
|
|
"""
|
2014-12-29 11:35:00 +03:00
|
|
|
Given a model class, returns a `FieldInfo` instance, which is a
|
|
|
|
`namedtuple`, containing metadata about the various field types on the model
|
|
|
|
including information about their relationships.
|
2014-09-09 20:46:28 +04:00
|
|
|
"""
|
|
|
|
opts = model._meta.concrete_model._meta
|
|
|
|
|
2014-12-29 11:35:00 +03:00
|
|
|
pk = _get_pk(opts)
|
|
|
|
fields = _get_fields(opts)
|
|
|
|
forward_relations = _get_forward_relationships(opts)
|
|
|
|
reverse_relations = _get_reverse_relationships(opts)
|
|
|
|
fields_and_pk = _merge_fields_and_pk(pk, fields)
|
|
|
|
relationships = _merge_relationships(forward_relations, reverse_relations)
|
|
|
|
|
|
|
|
return FieldInfo(pk, fields, forward_relations, reverse_relations,
|
|
|
|
fields_and_pk, relationships)
|
|
|
|
|
|
|
|
|
|
|
|
def _get_pk(opts):
|
2014-09-09 20:46:28 +04:00
|
|
|
pk = opts.pk
|
|
|
|
while pk.rel and pk.rel.parent_link:
|
2014-12-29 11:35:00 +03:00
|
|
|
# If model is a child via multi-table inheritance, use parent's pk.
|
2014-09-09 20:46:28 +04:00
|
|
|
pk = pk.rel.to._meta.pk
|
|
|
|
|
2014-12-29 11:35:00 +03:00
|
|
|
return pk
|
|
|
|
|
|
|
|
|
|
|
|
def _get_fields(opts):
|
2014-11-06 15:00:30 +03:00
|
|
|
fields = OrderedDict()
|
2014-09-09 20:46:28 +04:00
|
|
|
for field in [field for field in opts.fields if field.serialize and not field.rel]:
|
|
|
|
fields[field.name] = field
|
|
|
|
|
2014-12-29 11:35:00 +03:00
|
|
|
return fields
|
|
|
|
|
|
|
|
|
|
|
|
def _get_forward_relationships(opts):
|
|
|
|
"""
|
|
|
|
Returns an `OrderedDict` of field names to `RelationInfo`.
|
|
|
|
"""
|
2014-11-06 15:00:30 +03:00
|
|
|
forward_relations = OrderedDict()
|
2014-09-09 20:46:28 +04:00
|
|
|
for field in [field for field in opts.fields if field.serialize and field.rel]:
|
|
|
|
forward_relations[field.name] = RelationInfo(
|
2014-09-18 14:20:56 +04:00
|
|
|
model_field=field,
|
2014-09-09 20:46:28 +04:00
|
|
|
related=_resolve_model(field.rel.to),
|
|
|
|
to_many=False,
|
|
|
|
has_through_model=False
|
|
|
|
)
|
|
|
|
|
|
|
|
# Deal with forward many-to-many relationships.
|
|
|
|
for field in [field for field in opts.many_to_many if field.serialize]:
|
|
|
|
forward_relations[field.name] = RelationInfo(
|
2014-09-18 14:20:56 +04:00
|
|
|
model_field=field,
|
2014-09-09 20:46:28 +04:00
|
|
|
related=_resolve_model(field.rel.to),
|
|
|
|
to_many=True,
|
|
|
|
has_through_model=(
|
|
|
|
not field.rel.through._meta.auto_created
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2014-12-29 11:35:00 +03:00
|
|
|
return forward_relations
|
|
|
|
|
|
|
|
|
|
|
|
def _get_reverse_relationships(opts):
|
|
|
|
"""
|
|
|
|
Returns an `OrderedDict` of field names to `RelationInfo`.
|
|
|
|
"""
|
2014-11-06 15:00:30 +03:00
|
|
|
reverse_relations = OrderedDict()
|
2014-09-09 20:46:28 +04:00
|
|
|
for relation in opts.get_all_related_objects():
|
|
|
|
accessor_name = relation.get_accessor_name()
|
|
|
|
reverse_relations[accessor_name] = RelationInfo(
|
2014-09-18 14:20:56 +04:00
|
|
|
model_field=None,
|
2014-09-09 20:46:28 +04:00
|
|
|
related=relation.model,
|
|
|
|
to_many=relation.field.rel.multiple,
|
|
|
|
has_through_model=False
|
|
|
|
)
|
|
|
|
|
|
|
|
# Deal with reverse many-to-many relationships.
|
|
|
|
for relation in opts.get_all_related_many_to_many_objects():
|
|
|
|
accessor_name = relation.get_accessor_name()
|
|
|
|
reverse_relations[accessor_name] = RelationInfo(
|
2014-09-18 14:20:56 +04:00
|
|
|
model_field=None,
|
2014-09-09 20:46:28 +04:00
|
|
|
related=relation.model,
|
|
|
|
to_many=True,
|
|
|
|
has_through_model=(
|
2014-10-08 19:09:37 +04:00
|
|
|
(getattr(relation.field.rel, 'through', None) is not None)
|
|
|
|
and not relation.field.rel.through._meta.auto_created
|
2014-09-09 20:46:28 +04:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2014-12-29 11:35:00 +03:00
|
|
|
return reverse_relations
|
|
|
|
|
|
|
|
|
|
|
|
def _merge_fields_and_pk(pk, fields):
|
2014-11-06 15:00:30 +03:00
|
|
|
fields_and_pk = OrderedDict()
|
2014-09-18 14:20:56 +04:00
|
|
|
fields_and_pk['pk'] = pk
|
|
|
|
fields_and_pk[pk.name] = pk
|
|
|
|
fields_and_pk.update(fields)
|
|
|
|
|
2014-12-29 11:35:00 +03:00
|
|
|
return fields_and_pk
|
2014-09-18 14:20:56 +04:00
|
|
|
|
2014-12-29 11:35:00 +03:00
|
|
|
|
|
|
|
def _merge_relationships(forward_relations, reverse_relations):
|
|
|
|
return OrderedDict(
|
2014-09-18 14:20:56 +04:00
|
|
|
list(forward_relations.items()) +
|
|
|
|
list(reverse_relations.items())
|
|
|
|
)
|