How to know the key of a DRF Serializer DictField from within that field's child?

Viewed 255

Let's say we have some Django Rest Framework Serializer (we'll call it "serializer") that has a custom DictField as one of its fields. Within that custom DictField, the child field is tasked with recording the dictionary key that it is the value of whenever serializer.save() is called.

How would one go about knowing the key of the dictionary being deserialized when interpreting the internal value to be saved?

class ChildField(Serializer):
    parent_key = CharField()

    def to_internal_value(self, data):
        # At this point, the likely candidate for the
        # key is self.field_value, but it is actually ''
        # data['parent_key'] = ???

        return super().to_internal_value(data)

class ParentField(DictField):
    child = ChildField()

Assume the data being deserialized is the following:

{
    "the_dictionary": {
        "key_we_want_to_save": {
            "arbitrary_other_child_data": True
         }
    }
}
0 Answers
Related