Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django Rest Framework Dictionary Field

I'm using Mongodb with mongoengine as a backend for a API in Django. The framework I'm using to create the api is Django Rest Framework.

I need to store a dictionary in a field in Mongo and the best I've done when the method post is called is to use a charfield and parse the dictionary in the function restore_object.

There is a better way to achieve this goal?

It's better to create a dict field? I don't know how hard this could be.

Thank you.

edited to show some code, notice that I store the dictionary as a dict (DictField) and it's content could change from one object to other.

my mongoengine model is something like:

class MyDoc(mongoengine.Document):
    name = mongoengine.StringField(max_length=200)
    context = mongoengine.DictField()

and my serializer something like:

class MyDocSerializer(serializers.Serializer):

    name = serializers.CharField(max_length=200)
    context = serializers.CharField()
    url = serializers.HyperlinkedIdentityField(
    view_name="drf:mydoc-detail",)

    def __init__(self,*args,**kwargs):
    super(MyDocSerializer,self).__init__(*args,**kwargs)


    def restore_object(self, attrs, instance=None):

    # Parse string to dict
    # this is so ugly, notice I had to repace ' for " to
    # avoid an error parsing the json
    context = JSONParser().parse(
    StringIO.StringIO(
        attrs['context'].replace("'","\"")
        )
    )

    attrs['context'] = context
    if instance is not None:
        instance.name = attrs['name']
        instance.context = context
        return instance

    return MyDoc(**attrs)
like image 326
javier Avatar asked Jan 31 '13 13:01

javier


People also ask

What is Read_only field in Django?

read_only. Read-only fields are included in the API output, but should not be included in the input during create or update operations. Any 'read_only' fields that are incorrectly included in the serializer input will be ignored.

What is Serializers SerializerMethodField ()?

SerializerMethodField. This is a read-only field. It gets its value by calling a method on the serializer class it is attached to. It can be used to add any sort of data to the serialized representation of your object. Signature: SerializerMethodField(method_name=None)

What is DictField?

DictField is basically a dictionary field that validates the input against a dictionary of objects. It has the following arguments – child – A field instance that should be used for validating the values in the dictionary. If this argument is not provided then values in the mapping will not be validated.

How do you pass extra context data to Serializers in Django REST framework?

In function-based views, we can pass extra context to serializer with “context” parameter with a dictionary. To access the extra context data inside the serializer we can simply access it with “self. context”. From example, to get “exclude_email_list” we just used code 'exclude_email_list = self.


1 Answers

Rather than deal with the dictionary field in the Serializer's restore_object, you'll probably end up with something slightly cleaner, if instead you use a custom field for the dictionary field, that manages converting between the dictionary representation and internal char based storage.

You'll want to subclass serializers.WritableField and override the to_native() and from_native methods.

Relevant docs here.


Note: WritableField class that was present in version 2.x no longer exists. You should subclass Field and override to_internal_value() if the field supports data input.


Update: As of 3.0.4 you can now use serializers.DictField... http://www.django-rest-framework.org/api-guide/fields/#dictfield

like image 166
Tom Christie Avatar answered Sep 20 '22 13:09

Tom Christie