Giter Site home page Giter Site logo

claytondaley / drf-keyed-list Goto Github PK

View Code? Open in Web Editor NEW
9.0 9.0 4.0 22 KB

A reference implementation for converting a list of objects into a dict where the key is a (unique) value from the object (e.g. {<id1>: {<object1>}, <id2>: {<object2>}... }

License: Apache License 2.0

Python 100.00%

drf-keyed-list's People

Contributors

claytondaley avatar sshishov avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

drf-keyed-list's Issues

Exception if keys are UUID

I noticed this problem during applying the solution: if the key is UUID then the response dict is populated as UUID values which are not allowed in json.dumps function.
Python3 produces this exceptions:

.../3.7/lib/python3.7/json/encoder.py", line 257, in iterencode
    return _iterencode(o, 0)
TypeError: keys must be str, int, float, bool or None, not UUID

Python2 produces this:

.../2.7/lib/python2.7/json/encoder.py", line 270, in iterencode
    return _iterencode(o, 0)
TypeError: keys must be a string

I do not know what should we do here. Either we should convert all not supported values to str or maybe leave this on to being handled by custom JsonEncoder?!

AttributeError: 'list' object has no attribute 'items'

Oops, I think something is probs broken. I am using python 3.7 and django 2.15.

Following prescription, I am just doing this:
a = UserSerializer(User.objects.all(), many=True).data

Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "C:\Users\e\.virtualenvs\pinx-atTbH4Fl\lib\site-packages\rest_framework\serializers.py", line 765, in data
    ret = super(ListSerializer, self).data
  File "C:\Users\e\.virtualenvs\pinx-atTbH4Fl\lib\site-packages\rest_framework\serializers.py", line 262, in data
    self._data = self.to_representation(self.instance)
  File "C:\Users\e\.virtualenvs\pinx-atTbH4Fl\lib\site-packages\drf_keyed_list\serializers.py", line 19, in to_representation
    return {v.pop(self._keyed_field): v for k, v in response.items()}
AttributeError: 'list' object has no attribute 'items'

It seems that .items() is a dict method however the serializer is returning a list. I think it's a minor problem, but just haven't figure out how to modify the code...

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.