# Path: hedwig/rest_framework/serializers.py
# class HedwigModelSerializer(ModelSerializer):
#     def save(self, **kwargs):
#         action = 'created'
#         if self.instance is not None:
#             action = 'updated'
#         super(HedwigModelSerializer, self).save(**kwargs)
#         emit_hedwig_serializer_data(action, self.__module__.split('.')[0], self.__class__.__name__, self)
#         return self.instance
# 
# Path: tests/djangotest/chidiya/models.py
# class BasicTest(BaseTest):
# 
#     def natural_key(self):
#         return self.char, self.int, self.nullable, self.date
# 
# class HiddenTest(BaseTest):
#     class Hedwig:
#         fields = False
# 
# class ForeignKeyTest(BaseTest):
#     class Hedwig:
#         fields = ('char', 'int', 'basic',)
#     basic = models.ForeignKey(BasicTest)
# 
# class ManyToManyTest(BaseTest):
#     basic = models.ManyToManyField(BasicTest)
#     uuid = models.UUIDField()
#     url = models.URLField()


from rest_framework import serializers
from hedwig.rest_framework.serializers import HedwigModelSerializer
from .models import BasicTest, HiddenTest, ForeignKeyTest, ManyToManyTest
from django.db import models
class ForeignKeyTestSerializer(HedwigModelSerializer):
    basic = BasicTestSerializer()

    class Meta:
        model = ForeignKeyTest
        fields = (
            'id',
            'char',
            'int',
            'nullable',
            'date',
            'basic',
        )

    def create(self, validated_data, **kwargs):
        basic_data = validated_data.pop('basic', None)
        if basic_data is None:
            raise serializers.ValidationError("Basic chahiye")
        basic = None
        print basic_data
        if 'id' in basic_data:
            try:
                basic = BasicTest.objects.get(id=basic_data.get('id'))
            except models.ObjectDoesNotExist as e:
                pass
        if basic is None:
            basic_serializer = BasicTestSerializer(data=basic_data)
            basic_serializer.is_valid()
            basic = basic_serializer.save()


# Path: hedwig/rest_framework/serializers.py
# class HedwigModelSerializer(ModelSerializer):
#     def save(self, **kwargs):
#         action = 'created'
#         if self.instance is not None:
#             action = 'updated'
#         super(HedwigModelSerializer, self).save(**kwargs)
#         emit_hedwig_serializer_data(action, self.__module__.split('.')[0], self.__class__.__name__, self)
#         return self.instance
# 
# Path: tests/djangotest/chidiya/models.py
# class BasicTest(BaseTest):
# 
#     def natural_key(self):
#         return self.char, self.int, self.nullable, self.date
# 
# class HiddenTest(BaseTest):
#     class Hedwig:
#         fields = False
# 
# class ForeignKeyTest(BaseTest):
#     class Hedwig:
#         fields = ('char', 'int', 'basic',)
#     basic = models.ForeignKey(BasicTest)
# 
# class ManyToManyTest(BaseTest):
#     basic = models.ManyToManyField(BasicTest)
#     uuid = models.UUIDField()
#     url = models.URLField()


# Path: tests/djangotest/chidiya/serializers.py
from rest_framework import serializers
from hedwig.rest_framework.serializers import HedwigModelSerializer
from .models import BasicTest, HiddenTest, ForeignKeyTest, ManyToManyTest
from django.db import models

class ForeignKeyTestSerializer(HedwigModelSerializer):
    basic = BasicTestSerializer()

    class Meta:
        model = ForeignKeyTest
        fields = (
            'id',
            'char',
            'int',
            'nullable',
            'date',
            'basic',
        )

    def create(self, validated_data, **kwargs):
        basic_data = validated_data.pop('basic', None)
        if basic_data is None:
            raise serializers.ValidationError("Basic chahiye")
        basic = None
        print basic_data
        if 'id' in basic_data:
            try:
                basic = BasicTest.objects.get(id=basic_data.get('id'))
            except models.ObjectDoesNotExist as e:
                pass
        if basic is None:
            basic_serializer = BasicTestSerializer(data=basic_data)
            basic_serializer.is_valid()
            basic = basic_serializer.save()


        return ForeignKeyTest.objects.create(