orders.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. from rest_framework import serializers
  2. from metaservicesynced.models import Orders
  3. from tickets.models import Ticket, TicketList
  4. import traceback
  5. class OrdersSerializer(serializers.ModelSerializer):
  6. class Meta:
  7. model = Orders
  8. fields = '__all__'
  9. def create(self, validated_data):
  10. ModelClass = self.Meta.model
  11. if "ticket" not in validated_data:
  12. # Create ticket with minimal required data
  13. ticket_data = {
  14. "ticket_list": TicketList.objects.get(pk=9), # MS-SUP входящие обычные заявки ST_REQUEST
  15. "status": None,
  16. "created_date": "2000-01-01", # mandatory
  17. "status_changed_date": None,
  18. "created_by": None,
  19. "priority": 0, # default: 0
  20. "type": None,
  21. "title": "New ticket title", # mandatory
  22. "note": None,
  23. "due_date": None,
  24. "assigned_to": None,
  25. }
  26. ticket_instance = Task._default_manager.create(**task_data)
  27. validated_data["ticket"] = ticket_instance
  28. try:
  29. instance = ModelClass._default_manager.create(**validated_data)
  30. except TypeError:
  31. tb = traceback.format_exc()
  32. msg = (
  33. 'Got a `TypeError` when calling `%s.%s.create()`. '
  34. 'This may be because you have a writable field on the '
  35. 'serializer class that is not a valid argument to '
  36. '`%s.%s.create()`. You may need to make the field '
  37. 'read-only, or override the %s.create() method to handle '
  38. 'this correctly.\nOriginal exception was:\n %s' %
  39. (
  40. ModelClass.__name__,
  41. ModelClass._default_manager.name,
  42. ModelClass.__name__,
  43. ModelClass._default_manager.name,
  44. self.__class__.__name__,
  45. tb
  46. )
  47. )
  48. raise TypeError(msg)
  49. return instance