Created
September 24, 2014 11:18
-
-
Save eofs/573699a6e907626be2e2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.db import models | |
from jsonfield import JSONField | |
class ModelA(models.Model): | |
data = models.IntegerField() | |
extra = JSONField |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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