# Path: rest/model_tools.py
# def get_ct(model, for_concrete_model=False):
#     from .models import ContentType
#     if isinstance(model, str):
#         ctype = ContentType.objects.get_by_identifier(model)
#     else:
#         ctype = ContentType.objects.get_for_model(
#             model, for_concrete_model=for_concrete_model
#         )
#         # get_for_model sometimes returns a DjangoContentType - caching issue?
#         if not isinstance(ctype, ContentType):
#             ctype = ContentType.objects.get(pk=ctype.pk)
#             ContentType.objects._add_to_cache(ContentType.objects.db, ctype)
#     return ctype
# 
# def get_object_id(instance):
#     ct = get_ct(instance)
#     config = ct.get_config()
#     if config and 'lookup' in config:
#         return getattr(instance, config['lookup'])
#     return instance.pk
# 
# def get_by_identifier(queryset, ident):
#     if hasattr(queryset, 'get_by_identifier'):
#         return queryset.get_by_identifier(ident)
#     else:
#         ct = get_ct(queryset.model)
#         config = ct.get_config()
#         if config and 'lookup' in config:
#             lookup = config['lookup']
#         else:
#             lookup = 'pk'
#         return queryset.get(**{lookup: ident})


from django.contrib.contenttypes.models import (
    ContentType as DjangoContentType,
    ContentTypeManager as DjangoContentTypeManager
)
from django.utils.encoding import force_str
from .model_tools import get_ct, get_object_id, get_by_identifier
        from . import router
        from . import router  # avoid circular import
        from . import router  # avoid circular import
        return urlbase.replace(' ', '')

    # Get foreign keys for this content type
    def get_foreign_keys(self):
        cls = self.model_class()
        if cls is None:
            return []
        parents = {}
        for f in cls._meta.fields:
            rel = f.remote_field
            if rel is not None and type(rel).__name__ == 'ManyToOneRel':
                parent = get_ct(rel.model)
                parents.setdefault(parent, [])
                parents[parent].append(f.name)
        return parents

    def get_parents(self):
        return set(self.get_foreign_keys().keys())

    def get_children(self, include_rels=False):
        cls = self.model_class()
        if cls is None:
            return []

        rels = [
            field for field in cls._meta.get_fields()
            if (field.one_to_many or field.one_to_one)
            and field.auto_created and not field.concrete
        ]


# Path: rest/model_tools.py
# def get_ct(model, for_concrete_model=False):
#     from .models import ContentType
#     if isinstance(model, str):
#         ctype = ContentType.objects.get_by_identifier(model)
#     else:
#         ctype = ContentType.objects.get_for_model(
#             model, for_concrete_model=for_concrete_model
#         )
#         # get_for_model sometimes returns a DjangoContentType - caching issue?
#         if not isinstance(ctype, ContentType):
#             ctype = ContentType.objects.get(pk=ctype.pk)
#             ContentType.objects._add_to_cache(ContentType.objects.db, ctype)
#     return ctype
# 
# def get_object_id(instance):
#     ct = get_ct(instance)
#     config = ct.get_config()
#     if config and 'lookup' in config:
#         return getattr(instance, config['lookup'])
#     return instance.pk
# 
# def get_by_identifier(queryset, ident):
#     if hasattr(queryset, 'get_by_identifier'):
#         return queryset.get_by_identifier(ident)
#     else:
#         ct = get_ct(queryset.model)
#         config = ct.get_config()
#         if config and 'lookup' in config:
#             lookup = config['lookup']
#         else:
#             lookup = 'pk'
#         return queryset.get(**{lookup: ident})


# Path: rest/models.py
from django.contrib.contenttypes.models import (
    ContentType as DjangoContentType,
    ContentTypeManager as DjangoContentTypeManager
)
from django.utils.encoding import force_str
from .model_tools import get_ct, get_object_id, get_by_identifier
        from . import router
        from . import router  # avoid circular import
        from . import router  # avoid circular import

        return urlbase.replace(' ', '')

    # Get foreign keys for this content type
    def get_foreign_keys(self):
        cls = self.model_class()
        if cls is None:
            return []
        parents = {}
        for f in cls._meta.fields:
            rel = f.remote_field
            if rel is not None and type(rel).__name__ == 'ManyToOneRel':
                parent = get_ct(rel.model)
                parents.setdefault(parent, [])
                parents[parent].append(f.name)
        return parents

    def get_parents(self):
        return set(self.get_foreign_keys().keys())

    def get_children(self, include_rels=False):
        cls = self.model_class()
        if cls is None:
            return []

        rels = [
            field for field in cls._meta.get_fields()
            if (field.one_to_many or field.one_to_one)
            and field.auto_created and not field.concrete
        ]


        children = [(get_ct(rel.related_model), rel) for rel in rels]