Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support PostgreSQL JSONField #3170

Closed
ghost opened this issue Jul 18, 2015 · 4 comments
Closed

Support PostgreSQL JSONField #3170

ghost opened this issue Jul 18, 2015 · 4 comments

Comments

@ghost
Copy link

ghost commented Jul 18, 2015

hi thank you very much
,
DRF not support JSONField, but Django 1.9 support JSONField

url: https://docs.djangoproject.com/en/dev/ref/contrib/postgres/fields/#jsonfield

i use django 1.9 ,DRF 3.13 ,Postgresql 9.4

My database table fields: [i use postgresql]

 id<int> , jsonInfo<jsonb>

1,{"username": "dddd","pass":"fffff"}
2,{"username": "1111","pass":"22222"}
3,{"username": "33333","pass":"444444"}

i created custom JSONField in DRF


class JSONField(serializers.ReadOnlyField):

    def to_native(self, obj):
        return json.dumps(obj)

    def from_native(self, value):
        return json.loads(value)

My Serializer:


class tradeSerializer(serializers.HyperlinkedModelSerializer):
    id = serializers.IntegerField(read_only=True)
    username= JSONField(source='jsonInfo__username')

then :

i run

curl -H 'Accept: application/json; indent=4' -u admin:password http://127.0.0.1:8000/tr/

i can get 'id' field value, but i canot get username value, it's return null


if i changed My Serializer code like this:


class tradeSerializer(serializers.HyperlinkedModelSerializer):
    id = serializers.IntegerField(read_only=True)
    username= JSONField(source='jsonInfo')

i run

curl -H 'Accept: application/json; indent=4' -u admin:password http://127.0.0.1:8000/tr/

is it ok!
but it's return all jsonInfo field value

i hope , i can use JSONField(source='jsonInfo__username')

i only want get username

can you help me ?

thank you very much

:)

@carltongibson
Copy link
Collaborator

According to the Roadmap https://code.djangoproject.com/wiki/Version1.9Roadmap the alpha for 1.9 is not due for another two months. Final release is a while after that. DRF support for JSONField will be in line with that.

@carltongibson carltongibson changed the title hi,not support JSONField Support PostgreSQL JSONField (Django 1.9) Jul 18, 2015
@ghost
Copy link
Author

ghost commented Jul 18, 2015

@carltongibson thank you :)

@tomchristie tomchristie changed the title Support PostgreSQL JSONField (Django 1.9) Support PostgreSQL JSONField Jul 22, 2015
@tomchristie
Copy link
Member

There's still at least some work to do here...

  • We probably want to validate that only valid JSON native datatypes are present in the data structure.
  • We may want to treat string and binary inputs as raw JSON and parse them.
  • We need to map the JSONField model field onto a serializer field by default.

But yes, using DictField will give you 90% of what you need already.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants
@carltongibson @tomchristie and others