django-rest-framework icon indicating copy to clipboard operation
django-rest-framework copied to clipboard

Serializer fields for dictionaries and lists

Open thedrow opened this issue 13 years ago • 18 comments

I don't see an option in the API to serialize dictionaries or lists although they are representable by non-model objects. I also don't see a way to implement a custom field serializer through the current API. It should be in the core anyway in my opinion.

thedrow avatar Jan 06 '13 16:01 thedrow

I'm stumbling through this as well. How would I create a field that just holds a list of strings? I have a m2m relationship in my models, but I just want to expose a list of strings through the API to make things simpler for my client app.

erickyellott avatar May 25 '13 21:05 erickyellott

+1

Django Models is not my use case. I am using Django-Rest-Framework to act as an API Endpoint for a lot of data stored in a NoSQL DBMS. This would make development significantly easier and less "hackish".

kurtismullins avatar Jun 17 '13 19:06 kurtismullins

We are also using it for non-django data.

timc3 avatar Jun 28 '13 12:06 timc3

I think that this SerializerMethodField can solve your issue @thedrow.

dulacp avatar Jun 28 '13 18:06 dulacp

Can I ask what the ideal API for this would be? As @dulaccc mentioned, the SerializerMethodField can already return arbitrary python objects. What are the problems with that approach that you might expect this issue to address?

donspaulding avatar Sep 06 '13 17:09 donspaulding

As @dulaccc mentioned, the SerializerMethodField can already return arbitrary python objects. What are the problems with that approach that you might expect this issue to address?

SerializerMethodField is read-only, so I imagine they're looking for a read-write field.

rouge8 avatar Sep 06 '13 18:09 rouge8

Wrt to API I'd suggest something like ListField(contains=IntegerField). 'contains' could be optional.

On 6 Sep 2013, at 18:35, Don Spaulding [email protected] wrote:

Can I ask what the ideal API for this would be? As @dulaccc mentioned, the SerializerMethodField can already return arbitrary python objects. What are the problems with that approach that you might expect this issue to address?

— Reply to this email directly or view it on GitHub.

lovelydinosaur avatar Sep 06 '13 18:09 lovelydinosaur

I have been handling lists/arrays as follows which seems to work ok

class PGArrayField(serializers.WritableField):
    def to_native(self, obj):
        return json.dumps(obj)
    def from_native(self, obj):
        return json.loads(obj)

class PGHstoreField(serializers.WritableField):
    def to_native(self, obj):
        return json.dumps(obj)
    def from_native(self, obj):
        return json.loads(obj)

class SomeModel(models.model):
    attributes = DictionaryField(blank=True)
    aliases = ArrayField(dbtype="text",blank=True)

class SomeModelSerializer(serializers.ModelSerializer):
    attributes = PGHstoreField(required=False)
    aliases = PGArrayField(required=False)

    class Meta:
        model = SomeModel
        fields = ('id', 'attributes',  'aliases',)

gkappel avatar Sep 07 '13 15:09 gkappel

@gkappel how/where are you DictionaryField & ArrayField defined?

cummingscs avatar Dec 03 '13 16:12 cummingscs

from djorm_hstore.fields import DictionaryField from djorm_pgarray.fields import ArrayField

See https://github.com/niwibe/djorm-ext-hstore https://github.com/niwibe/djorm-ext-pgarray

gkappel avatar Dec 03 '13 17:12 gkappel

@estebistec: You may already have figured this, but note that those are model fields, not serializer fields.

lovelydinosaur avatar Dec 03 '13 17:12 lovelydinosaur

@tomchristie Doh! No I missed that detail. Thanks.

cummingscs avatar Dec 03 '13 17:12 cummingscs

BTW, long-term, my goal would be to generate serializers from schematics models to provide the convenience of model serializers but the flexibility needed for non-RDBMS data sources. I don't expect that to land here, it'll be a separate github project. But container types here could/would support it.

https://github.com/j2labs/schematics

cummingscs avatar Dec 03 '13 18:12 cummingscs

I'm thinking of something as simple as this for this issue: https://gist.github.com/estebistec/7775918 Is that more or less what others had in mind?

cummingscs avatar Dec 03 '13 19:12 cummingscs

I'm thinking of something as simple as this for this issue: https://gist.github.com/estebistec/7775918 Is that more or less what others had in mind?

That's the idea, yup

lovelydinosaur avatar Dec 03 '13 20:12 lovelydinosaur

Actually, it appears BaseSerializer changes the signature of from_native to include a files parameter. So I'll have to do some finagling there before I submit a pull request.

cummingscs avatar Dec 04 '13 04:12 cummingscs

Created pull #1285 to implement this.

cummingscs avatar Dec 12 '13 06:12 cummingscs

Closing pending update on third party package from @estebistec.

lovelydinosaur avatar Jan 03 '14 12:01 lovelydinosaur