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.
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.
+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".
We are also using it for non-django data.
I think that this SerializerMethodField can solve your issue @thedrow.
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?
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.
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.
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 how/where are you DictionaryField & ArrayField defined?
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
@estebistec: You may already have figured this, but note that those are _model_ fields, not _serializer_ fields.
@tomchristie Doh! No I missed that detail. Thanks.
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.
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?
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
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.
Created pull #1285 to implement this.
Closing pending update on third party package from @estebistec.
Most helpful comment
I have been handling lists/arrays as follows which seems to work ok