Skip to content

Instantly share code, notes, and snippets.

@eofs
Created September 24, 2014 11:18
Show Gist options
  • Save eofs/573699a6e907626be2e2 to your computer and use it in GitHub Desktop.
Save eofs/573699a6e907626be2e2 to your computer and use it in GitHub Desktop.
from django.db import models
from jsonfield import JSONField
class ModelA(models.Model):
data = models.IntegerField()
extra = JSONField
from app.models import ModelA
class ExtraSerializer(serializers.Serializer):
fieldA = serializers.CharField()
fieldB = serializers.CharField()
fieldC = serializers.CharField()
def validate_fieldA(self, attrs, source):
# Validate and raise ValidationError
pass
class SerializerA(serializers.ModelSerializer):
extra = ExtraSerializer(write_only=True)
class Meta:
model = ModelA
def save_object(self, obj, **kwargs):
# Hacky hacky!
obj._nested_forward_relations.pop('extra')
return super(SerializerA, self).save_object(obj, **kwargs)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment