# -*- coding: utf-8 -*-
from setuptools import setup

packages = \
['rest_typed_models']

package_data = \
{'': ['*']}

setup_kwargs = {
    'name': 'django-rest-typed-models',
    'version': '0.1.0',
    'description': 'Typed model serializers for Django REST Framework.',
    'long_description': '[![image](https://travis-ci.org/apirobot/django-rest-typed-models.svg?branch=master)](https://travis-ci.org/apirobot/django-rest-typed-models)\n\n[![image](https://codecov.io/gh/apirobot/django-rest-typed-models/branch/master/graph/badge.svg)](https://codecov.io/gh/apirobot/django-rest-typed-models)\n\n[![image](https://badge.fury.io/py/django-rest-typed-models.svg)](https://badge.fury.io/py/django-rest-typed-models)\n\n  --------------------------\n  Django REST Typed models\n  --------------------------\n\nBased on the great work django-rest-typed-models by denisorehovsky\n\nTyped model serializers for Django REST Framework.\n\nOverview\n========\n\n`django-rest-typed-models` allows you to easily define serializers for\nyour inherited models that you have created using `django-typed-models`\nlibrary.\n\nInstallation\n============\n\nInstall using `pip`:\n\n``` {.bash}\n$ poetry add django-rest-typed-models\n```\n\nUsage\n=====\n\nDefine your typed models:\n\n``` {.python}\n# models.py\nfrom django.db import models\nfrom typedmodels.models import TypedModel\n\nclass Project(TypedModel):\n    topic = models.CharField(max_length=30)\n\n\nclass ArtProject(Project):\n    artist = models.CharField(max_length=30)\n\n\nclass ResearchProject(Project):\n    supervisor = models.CharField(max_length=30)\n```\n\nDefine serializers for each typed model the way you did it when you used\n`django-rest-framework`:\n\n``` {.python}\n# serializers.py\nfrom rest_framework import serializers\nfrom .models import Project, ArtProject, ResearchProject\n\n\nclass ProjectSerializer(serializers.ModelSerializer):\n    class Meta:\n        model = Project\n        fields = (\'topic\', )\n\n\nclass ArtProjectSerializer(serializers.HyperlinkedModelSerializer):\n    class Meta:\n        model = ArtProject\n        fields = (\'topic\', \'artist\', \'url\')\n        extra_kwargs = {\n            \'url\': {\'view_name\': \'project-detail\', \'lookup_field\': \'pk\'},\n        }\n\n\nclass ResearchProjectSerializer(serializers.ModelSerializer):\n    class Meta:\n        model = ResearchProject\n        fields = (\'topic\', \'supervisor\')\n```\n\nNote that if you extend `HyperlinkedModelSerializer` instead of\n`ModelSerializer` you need to define `extra_kwargs` to direct the URL to\nthe appropriate view for your typed model serializer.\n\nThen you have to create a typed model serializer that serves as a mapper\nbetween models and serializers which you have defined above:\n\n``` {.python}\n# serializers.py\nfrom rest_typed_models.serializers import TypedModelSerializer\n\n\nclass ProjectTypedModelSerializer(TypedModelSerializer):\n    model_serializer_mapping = {\n        Project: ProjectSerializer,\n        ArtProject: ArtProjectSerializer,\n        ResearchProject: ResearchProjectSerializer\n    }\n```\n\nCreate viewset with serializer\\_class equals to your polymorphic\nserializer:\n\n``` {.python}\n# views.py\nfrom rest_framework import viewsets\nfrom .models import Project\nfrom .serializers import ProjectTypedModelSerializer\n\n\nclass ProjectViewSet(viewsets.ModelViewSet):\n    queryset = Project.objects.all()\n    serializer_class = ProjectTypedModelSerializer\n```\n\nTest it:\n\n``` {.bash}\n$ http GET "http://localhost:8000/projects/"\n```\n\n``` {.http}\nHTTP/1.0 200 OK\nContent-Length: 227\nContent-Type: application/json\n\n[\n    {\n        "resourcetype": "Project",\n        "topic": "John\'s gathering"\n    },\n    {\n        "artist": "T. Turner",\n        "resourcetype": "ArtProject",\n        "topic": "Sculpting with Tim",\n        "url": "http://localhost:8000/projects/2/"\n    },\n    {\n        "resourcetype": "ResearchProject",\n        "supervisor": "Dr. Winter",\n        "topic": "Swallow Aerodynamics"\n    }\n]\n```\n\n``` {.bash}\n$ http POST "http://localhost:8000/projects/" resourcetype="ArtProject" topic="Guernica" artist="Picasso"\n```\n\n``` {.http}\nHTTP/1.0 201 Created\nContent-Length: 67\nContent-Type: application/json\n\n{\n    "artist": "Picasso",\n    "resourcetype": "ArtProject",\n    "topic": "Guernica",\n    "url": "http://localhost:8000/projects/4/"\n}\n```\n\nCustomize resource type\n=======================\n\nAs you can see from the example above, in order to specify the type of\nyour typed model, you need to send a request with resource type\nfield. The value of resource type should be the name of the model.\n\nIf you want to change the resource type field name from `resourcetype`\nto something else, you should override `resource_type_field_name`\nattribute:\n\n``` {.python}\nclass ProjectTypedModelSerializer(TypedModelSerializer):\n    resource_type_field_name = \'projecttype\'\n    ...\n```\n\nIf you want to change the behavior of resource type, you should override\n`to_resource_type` method:\n\n``` {.python}\nclass ProjectTypedModelSerializer(TypedModelSerializer):\n    ...\n\n    def to_resource_type(self, model_or_instance):\n        return model_or_instance._meta.object_name.lower()\n```\n\nNow, the request for creating new object will look like this:\n\n``` {.bash}\n$ http POST "http://localhost:8000/projects/" projecttype="artproject" topic="Guernica" artist="Picasso"\n```\n',
    'author': 'Krystof Beuermann',
    'author_email': 'krystof+django@blackbox.ms',
    'maintainer': 'None',
    'maintainer_email': 'None',
    'url': 'None',
    'packages': packages,
    'package_data': package_data,
    'python_requires': '>=3.10,<4.0',
}


setup(**setup_kwargs)
