I am trying to create a logic to allow users Order Service, what i want to do is overide the post()
or create()
so i can be able to create a Notification Object while submitting the new order, but i keep getting this error that says post() got multiple values for argument 'seller_id'
class OrderService(generics.ListCreateAPIView):
serializer_class = OrderSerializer
queryset = Orders.objects.all()
def post(self):
seller_id = self.kwargs['seller_id']
buyer_id = self.kwargs['buyer_id']
service_id = self.kwargs['service_id']
seller = User.objects.get(id=seller_id)
buyer = User.objects.get(id=buyer_id)
service = Service.objects.get(id=service_id)
order = Orders.objects.create(seller=seller, buyer=buyer, service=service)
notification = BuyerNotification.objects.create(buyer=buyer, service=service, order=order, notification_type="awaiting_response")
order.save()
notification.save()
return order
Update 1
I found out that the post()
method need to take in self, request
, so i did that
def post(self, request ,seller_id ,buyer_id, service_id):
...
return Response({"order": order})
Now the error seems to be gone, but when i fill the form from the browser
And submit, it does get saved in the database but the form fields data does not get saved, it just saves a blank form despite the face that i have filled the form.
this is the respons i get after submitting the form
what could be causing this issue now?
Update 2
I figured i could use only the CreateAPIView
then overide the create()
method, instead of using the ListCreateAPIView
(which i would still want to know how to overide the post method)
This is the new code i wrote to create an order and also a notification instance, the problem is, when i submit the form, it still get saved to the database but with blank fields.
class OrderService(generics.CreateAPIView):
serializer_class = OrderSerializer
def create(self, request, *args, **kwargs):
seller_id = self.kwargs['seller_id']
buyer_id = self.kwargs['buyer_id']
service_id = self.kwargs['service_id']
seller = User.objects.get(id=seller_id)
buyer = User.objects.get(id=buyer_id)
service = Service.objects.get(id=service_id)
order = Orders.objects.create(seller=seller, buyer=buyer, service=service)
notification = BuyerNotification.objects.create(buyer=buyer, service=service, order=order, notification_type="awaiting_response")
order.save()
notification.save()
return Response(status=201)
Update 3
This updated code seems to be working, but how do i create a notification object in the already overridden create method
, how do i get the buyer instance
, seller instance
, service instance
to create a notification object.
class OrderService(generics.CreateAPIView):
serializer_class = OrderSerializer
def create(self, request, *args, **kwargs):
serializer = self.get_serializer(data=request.data)
serializer.is_valid(raise_exception=True)
self.perform_create(serializer)
headers = self.get_success_headers(serializer.data)
return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers)
This is my notification model
class BuyerNotification(models.Model):
buyer = models.ForeignKey(User, on_delete=models.SET_NULL, null=True, related_name="buyer_notifications")
service = models.ForeignKey(Service, on_delete=models.SET_NULL, null=True, related_name="buyer_service_notifications")
order = models.ForeignKey(Orders, on_delete=models.SET_NULL, null=True, related_name="buyer_service_order")
notification_type = models.CharField(max_length=100, choices=NOTIFICATION_TYPE, default="none")
is_read = models.BooleanField(default=False)
date = models.DateTimeField(auto_now_add=True)
nid = ShortUUIDField(length=10, max_length=25, alphabet="abcdefghijklmnopqrstuvxyz")