title: Serializer for JSONField nested representation
body: <p>In my application there's a model that stores a configuration in one of its fields. The field is defined as <code>JSONField</code>. I have a strict structure that defines what content of this field should look like, but I'm struggling to find a way how to serialize it to validate data in API requests.</p>
  
  <p>The solution that works for now, but does not validate what's inside <code>config_field</code>, is blindly accept anything that complies with being a <code>json</code> object:</p>
  
  <p>A simplified version of my model:</p>
  
  <pre><code>class MyModel(models.Model):
      config_field = JSONField(...)
      ...
  </code></pre>
  
  <p>For the sake of this question here's a simplified version of the data structure stored in <code>config_field</code>:</p>
  
  <pre><code>{"some_config_int": 42, "some_config_vars": [{"id": 1}, {"id": 2}]}
  </code></pre>
  
  <p>And here's a simplified version of my serializer:</p>
  
  <pre><code>class MyModelSerializer(serializers.ModelSerializer):
          config_field = serializers.JSONField(required=False)
          class Meta:
              model = MyModel
              fields = ('config_field', ...)
  </code></pre>
  
  <p>What I'd like to achieve though is to have a serializer for the nested representation (<a href="https://www.django-rest-framework.org/api-guide/serializers/#dealing-with-nested-objects" rel="nofollow noreferrer">reference to DRF documentation</a>) of what's inside <code>config_field</code>. What I've tried so far (but does not work):</p>
  
  <pre><code>class ConfigVarsSerializer(serializers.Serializer):
          id = serializers.IntegerField(required=True)
  
  class ConfigFieldsSerializer(serializers.Serializer):
      some_config_int = serializers.IntegerField(required=True)
      some_config_vars = serializers.ListField(child=ConfigVarsSerializer,required=True)
  
  class MyModelSerializer(serializers.ModelSerializer):
              config_field = ConfigFieldsSerializer(required=False)
              class Meta:
                  model = MyModel
                  fields = ('config_field', ...)
  </code></pre>
  
  <p>This way it will be optional to POST/PUT an object with a configuration, but if <code>config_field</code> is in the body of the request, the entire nested object should be provided.</p>