def create(self, validated_data): pid = validated_data.pop('payment', None) if pid: try: p = self.context['order'].payments.get(local_id=pid) except OrderPayment.DoesNotExist: raise ValidationError('Unknown payment ID.') else: p = None order = OrderRefund(order=self.context['order'], payment=p, **validated_data) order.save() return order