# 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
                'post': request.DATA
            },
            status=response.status_code,
            template_name=template
        )

    def destroy(self, request, *args, **kwargs):
        try:
            response = super(ModelViewSet, self).destroy(
                request, *args, **kwargs
            )
        except ProtectedError as e:
            return Response({
                'non_field_errors': [
                    e.args[0]
                ]
            }, status=400)
        else:
            return response

    def get_parent(self, ct, kwarg_name, response):
        pid = self.kwargs.get(kwarg_name, None)
        if not pid:
            return

        pcls = ct.model_class()
        if self.router:
            slug = self.router.get_lookup_for_model(pcls)
            parent = pcls.objects.get(**{slug: pid})
        else:

# 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

                'post': request.DATA
            },
            status=response.status_code,
            template_name=template
        )

    def destroy(self, request, *args, **kwargs):
        try:
            response = super(ModelViewSet, self).destroy(
                request, *args, **kwargs
            )
        except ProtectedError as e:
            return Response({
                'non_field_errors': [
                    e.args[0]
                ]
            }, status=400)
        else:
            return response

    def get_parent(self, ct, kwarg_name, response):
        pid = self.kwargs.get(kwarg_name, None)
        if not pid:
            return

        pcls = ct.model_class()
        if self.router:
            slug = self.router.get_lookup_for_model(pcls)
            parent = pcls.objects.get(**{slug: pid})
        else:

            parent = get_by_identifier(pcls.objects, pid)