Skip to content

Support PostgreSQL JSONField #3170

@ghost

Description

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

:)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions