def post(self, request, *args, **kwargs): serializer = self.get_serializer(data=request.data) serializer.is_valid(raise_exception=True) customer = Customer() customer.name = serializer.data['name'] customer.email = serializer.data['email'] customer.tax_id = serializer.data['tax_id'] customer.save() headers = self.get_success_headers(serializer.data) return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers)
def save(cls, data, instance=None): try: customer_name = data.get('customer_name', '') ticket = instance if instance else Ticket() with transaction.atomic(): for key, value in data.items(): setattr(ticket, key, value) if customer_name: if ticket.customer_id: customer = CustomerService.get(ticket.customer_id) else: customer = Customer() customer.name = customer_name customer.save() ticket.customer_id = customer.id ticket.save() return ticket except Exception as ex: # todo: handle log raise ex