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

Serializer fields for dictionaries and lists #560

Closed
thedrow opened this issue Jan 6, 2013 · 18 comments
Closed

Serializer fields for dictionaries and lists #560

thedrow opened this issue Jan 6, 2013 · 18 comments

Comments

@thedrow
Copy link
Contributor

thedrow commented Jan 6, 2013

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.

@boxy-robot
Copy link

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.

@kurtismullins
Copy link

+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".

@timc3
Copy link

timc3 commented Jun 28, 2013

We are also using it for non-django data.

@dulacp
Copy link
Contributor

dulacp commented Jun 28, 2013

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

@donspaulding
Copy link

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?

@rouge8
Copy link
Contributor

rouge8 commented Sep 6, 2013

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.

@tomchristie
Copy link
Member

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

On 6 Sep 2013, at 18:35, Don Spaulding notifications@github.com 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.

@gkappel
Copy link

gkappel commented Sep 7, 2013

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',)

@estebistec
Copy link
Contributor

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

@gkappel
Copy link

gkappel commented Dec 3, 2013

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

@tomchristie
Copy link
Member

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

@estebistec
Copy link
Contributor

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

@estebistec
Copy link
Contributor

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

@estebistec
Copy link
Contributor

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?

@tomchristie
Copy link
Member

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

@estebistec
Copy link
Contributor

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.

@estebistec
Copy link
Contributor

Created pull #1285 to implement this.

@tomchristie
Copy link
Member

Closing pending update on third party package from @estebistec.

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

10 participants