from rest_framework import serializers from metaservicesynced.models import Orders from tickets.models import Task, TaskList import traceback class OrdersSerializer(serializers.ModelSerializer): class Meta: model = Orders fields = '__all__' def create(self, validated_data): ModelClass = self.Meta.model if "ticket" not in validated_data: # Create ticket with minimal required data task_data = { "task_list": TaskList.objects.get(pk=9), # MS-SUP входящие обычные заявки ST_REQUEST "status": None, "created_date": "2000-01-01", # mandatory "status_changed_date": None, "created_by": None, "priority": 0, # default: 0 "type": None, "title": "New ticket title", # mandatory "note": None, "due_date": None, "assigned_to": None, } ticket_instance = Task._default_manager.create(**task_data) validated_data["ticket"] = ticket_instance try: instance = ModelClass._default_manager.create(**validated_data) except TypeError: tb = traceback.format_exc() msg = ( 'Got a `TypeError` when calling `%s.%s.create()`. ' 'This may be because you have a writable field on the ' 'serializer class that is not a valid argument to ' '`%s.%s.create()`. You may need to make the field ' 'read-only, or override the %s.create() method to handle ' 'this correctly.\nOriginal exception was:\n %s' % ( ModelClass.__name__, ModelClass._default_manager.name, ModelClass.__name__, ModelClass._default_manager.name, self.__class__.__name__, tb ) ) raise TypeError(msg) return instance