[Answered ]-Create child object in view as extra action

1👍

Solved, I can create it using .create() and afterwards adding it to the house object with vehicles.add()

Extra steps are required due to brand being a model itself in my case.

Solution

@action(methods=['post'], detail=True, url_name='create_vehicle')
    def create_vehicle(self, request,pk=None):
        house = self.get_object()
        brand = Brand.objects.get(id = request.data['brand'])
        vehicle = Vehicle.objects.create(license_plate=request.data['license_plate'], brand=brand)
        house.vehicles.add(vehicle)
        return Response({'msg':'Creation success'}, status=HTTP_200_OK)   

Leave a comment