# 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:
```python
from drf_keyed_list import KeyedListSerializer
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:
```python
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.
## Non-String Keys (e.g. UUIDs)
Per the [JSON RFC](https://tools.ietf.org/html/rfc7159.html#section-4) the keys (a.k.a. "names") in a JSON structure
must be strings. The JSON Encoder in Py2 only accepts strings; in Py3, the encoder accepts some additional types (i.e.
`int`, `float`, `bool` or `None`), but these must eventually be converted to strings. Other types are not supported,
including common key types like UUID.
Per the discussion in issue #6, the recommended strategy for non-string keys is to use an expicit (if necessary, custom)
serializer field. This field should implement `to_representation` and `to_internal_value` to convert the data to a
string. For a UUID, the built-in `UUIDField` is sufficient.
Authors
=======
2018, Clayton Daley III
Raw data
{
"_id": null,
"home_page": "http://github.com/bihealth/drf-keyed-list",
"name": "drf-keyed-list-bihealth",
"maintainer": "",
"docs_url": null,
"requires_python": "",
"maintainer_email": "",
"keywords": "drf restframework rest_framework django_rest_framework serializers drf_mapped_nested",
"author": "Manuel Holtgrewe, Mikko Nieminen, Oliver Stolpe",
"author_email": "manuel.holtgrewe@bih-charite.de, mikko.nieminen@bih-charite.de, oliver.stolpe@bih-charite.de",
"download_url": "https://files.pythonhosted.org/packages/72/47/0e5f9017741e29a691908cf9ba6fbbbc7e5991a8474cd615f698e1569875/drf-keyed-list-bihealth-0.2.1.tar.gz",
"platform": null,
"description": "# Keyed Lists for Django REST Framework\n\nThis package supports the serialization and deserialization of a list of objects stored in a `dict` where a unique\nvalue from the object (often a `pk`) is used as the key in the dict. For example,\n\n\n```\n{\n \"1\": {\n <other fields for object with id 1> \n },\n \"2\": {\n <other fields for object with id 2>\n },\n ...\n}\n```\n\n# Install\n\n```\npip install drf-keyed-list\n```\n\n# Usage\n\nThe following is a usage example:\n\n```python\nfrom drf_keyed_list import KeyedListSerializer\n\nclass MySerializer(ModelSerializer):\n\n class Meta:\n list_serializer_class = KeyedListSerializer\n keyed_list_serializer_field = 'id'\n```\n\nBy replacing the `list_serializer_class`, this behavior will only be enabled when the `many=True` flag is used:\n\n```python\ninstance = {\n \"id\": \"pk_val\",\n \"field1\": \"val1\",\n \"field2\": \"val2\",\n # ...\n}\n\nserializer = MySerializer(data=instance)\n# this should work\nserializer.is_valid()\nserializer.save()\n\nkeyed_list = {\n \"pk_val\": {\n \"field1\": \"val1\",\n \"field2\": \"val2\",\n # ...\n }\n}\n\n# many=True will trigger the keyed-list behavior\nserializer = MySerializer(data=keyed_list, many=True)\n# this should also work\nserializer.is_valid()\nserializer.save()\n```\n\nNOTE: `keyed_list_serializer_field` ***MUST*** refer to a Unique field or key collision may occur during serialization,\nplus undefined deserializaiton behavior if used in combination with nested writable serializers (e.g.\n[drf-writable-nested](https://github.com/beda-software/drf-writable-nested)). At this time, the package does not\nmake any effort to verify that a Unique field has been selected.\n\n## Non-String Keys (e.g. UUIDs)\n\nPer the [JSON RFC](https://tools.ietf.org/html/rfc7159.html#section-4) the keys (a.k.a. \"names\") in a JSON structure \nmust be strings. The JSON Encoder in Py2 only accepts strings; in Py3, the encoder accepts some additional types (i.e. \n`int`, `float`, `bool` or `None`), but these must eventually be converted to strings. Other types are not supported,\nincluding common key types like UUID.\n\nPer the discussion in issue #6, the recommended strategy for non-string keys is to use an expicit (if necessary, custom)\nserializer field. This field should implement `to_representation` and `to_internal_value` to convert the data to a \nstring. For a UUID, the built-in `UUIDField` is sufficient.\n\nAuthors\n=======\n2018, Clayton Daley III\n",
"bugtrack_url": null,
"license": "Apache 2.0",
"summary": "Fork of drf-keyed-list maintained by @bihealth",
"version": "0.2.1",
"project_urls": {
"Homepage": "http://github.com/bihealth/drf-keyed-list"
},
"split_keywords": [
"drf",
"restframework",
"rest_framework",
"django_rest_framework",
"serializers",
"drf_mapped_nested"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "72470e5f9017741e29a691908cf9ba6fbbbc7e5991a8474cd615f698e1569875",
"md5": "cc93f9e675fa09f973f75d808d561a1a",
"sha256": "c43f456b0190853a185ff7d5288306d3735d4cca1acc9b00a5ff1a04c7d85e6f"
},
"downloads": -1,
"filename": "drf-keyed-list-bihealth-0.2.1.tar.gz",
"has_sig": false,
"md5_digest": "cc93f9e675fa09f973f75d808d561a1a",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 7844,
"upload_time": "2024-02-29T14:34:15",
"upload_time_iso_8601": "2024-02-29T14:34:15.857641Z",
"url": "https://files.pythonhosted.org/packages/72/47/0e5f9017741e29a691908cf9ba6fbbbc7e5991a8474cd615f698e1569875/drf-keyed-list-bihealth-0.2.1.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-02-29 14:34:15",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "bihealth",
"github_project": "drf-keyed-list",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"requirements": [],
"lcname": "drf-keyed-list-bihealth"
}