How to create an object in Django Rest Framework that is related to other models

I have 3 models: user, barber and booking. In my createBooking view, I am having trouble to figure out how to pass the IDs of the user and the barber. Here's my code:

The view:

def createBooking(request):
    data = request.data
  
    booking = Booking.objects.create(
        barber_id = data['barberb_id'],
        customer_id = data[ 'customer_id'],
        timeslot = data['timeslot'],
        )
    serializer = BookingSerializer(booking, many=False)
    return Response(serializer.data)

The booking model:

    customer_id = models.ForeignKey(User, on_delete = models.CASCADE,)
    barber_id = models.ForeignKey(Barber, on_delete = models.CASCADE)
    timeslot = models.DateTimeField('appointment')

    def __str__(self):
        return f"{self.customer_id} {self.barber_id} {self.timeslot}"

With the current code, I'm getting the error Cannot assign "['customer_id']": "Booking.customer_id" must be a "User" instance.

Back to Top