# 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 rest_framework.generics import GenericAPIView as RestGenericAPIView
from rest_framework.response import Response
from rest_framework.decorators import action
from rest_framework import status, viewsets
from .model_tools import get_ct, get_object_id, get_by_identifier
from django.core.exceptions import FieldDoesNotExist
from django.db.models import ProtectedError
        if page != ct.identifier and self.router:
            # Optional: return to detail view of a parent model
            pconf = self.router.get_page_config(page)
            if pconf.get('list', None) and mode != "list":
                oid = response.data.get(page + '_id', None)
        else:
            # Default: return to detail view of the saved model
            pconf = conf
            if mode != "list":
                oid = response.data['id']

        url = "/" + pconf['url']
        if pconf['url'] and pconf.get('list', None):
            url += "/"
        if oid:
            url += str(oid)
            if mode == "edit":
                url += "/edit"

        return Response(
            {'detail': 'Created'},
            status=status.HTTP_302_FOUND,
            headers={'Location': url}
        )

    def saveerror(self, request, response):
        errors = [{
            'field': key,
            'errors': val
        } for key, val in response.data.items()]

# 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/views.py
from rest_framework.generics import GenericAPIView as RestGenericAPIView
from rest_framework.response import Response
from rest_framework.decorators import action
from rest_framework import status, viewsets
from .model_tools import get_ct, get_object_id, get_by_identifier
from django.core.exceptions import FieldDoesNotExist
from django.db.models import ProtectedError

        if page != ct.identifier and self.router:
            # Optional: return to detail view of a parent model
            pconf = self.router.get_page_config(page)
            if pconf.get('list', None) and mode != "list":
                oid = response.data.get(page + '_id', None)
        else:
            # Default: return to detail view of the saved model
            pconf = conf
            if mode != "list":
                oid = response.data['id']

        url = "/" + pconf['url']
        if pconf['url'] and pconf.get('list', None):
            url += "/"
        if oid:
            url += str(oid)
            if mode == "edit":
                url += "/edit"

        return Response(
            {'detail': 'Created'},
            status=status.HTTP_302_FOUND,
            headers={'Location': url}
        )

    def saveerror(self, request, response):
        errors = [{
            'field': key,
            'errors': val
        } for key, val in response.data.items()]

        template = get_ct(self.model).identifier + '_error.html'