-
-
Notifications
You must be signed in to change notification settings - Fork 6.8k
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
Comments
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? |
|
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:
|
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 See |
@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? |
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. |
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.
The text was updated successfully, but these errors were encountered: