Metadata-Version: 2.1
Name: drf-keyed-list
Version: 0.0.2
Summary: UNKNOWN
Home-page: http://github.com/claytondaley/drf-keyed-list
Author: Clayton Daley III
Author-email: clayton.daley+github@gmail.com
License: Apache 2.0
Keywords: drf restframework rest_framework django_rest_framework serializers drf_mapped_nested
Platform: UNKNOWN
Classifier: Development Status :: 3 - Alpha
Classifier: Environment :: Web Environment
Classifier: Framework :: Django
Classifier: Framework :: Django :: 1.9
Classifier: Framework :: Django :: 1.10
Classifier: Framework :: Django :: 1.11
Classifier: Framework :: Django :: 2.0
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: Apache Software License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.3
Classifier: Programming Language :: Python :: 3.4
Classifier: Programming Language :: Python :: 3.5
Classifier: Topic :: Internet :: WWW/HTTP
Description-Content-Type: text/markdown

# Keyed Lists for Django REST Framework

This package supports the serialization and deserialization of a list of objects stored in a `dict` where a unique
value from the object (often a `pk`) is used as the key in the dict.  For example,


```
{
    "1": {<other fields for object with id 1>},
    "2": {<other fields for object with id 2>},
    ...
}
```

# Install

`pip install drf-keyed-list`

# Usage

The following is a usage example:

```
class MySerializer(ModelSerializer):

    class Meta:
        list_serializer_class = KeyedListSerializer
        keyed_list_serializer_field = 'id'
```

By replacing the `list_serializer_class`, this behavior will only be enabled when the `many=True` flag is used:

```
instance = {
   "id": "pk_val",
   "field1": "val1",
   "field2": "val2",
   ...
}

serializer = MySerializer(data=instance)
# this should work
serializer.is_valid()
serializer.save()

keyed_list = {
   "pk_val": {
       "field1": "val1",
       "field2": "val2",
       ...
   }
}

# many=True will trigger the keyed-list behavior
serializer = MySerializer(data=keyed_list, many=True)
# this should also work
serializer.is_valid()
serializer.save()
```

NOTE: `keyed_list_serializer_field` ***MUST*** refer to a Unique field or key collision may occur during serialization,
plus undefined deserializaiton behavior if used in combination with nested writable serializers (e.g.
[drf-writable-nested](https://github.com/beda-software/drf-writable-nested)).  At this time, the package does not
make any effort to verify that a Unique field has been selected.

Authors
=======
2018, Clayton Daley III


