Open pjedur opened 7 years ago
I haven't quite got my head around exactly how this works, but DRF somehow builds a schema based on whether it can invoke endpoints or not, and this is the schema that django-rest-swagger
displays.
I guess what's happening here is:
get_queryset
gets called during the building of this schema;self.kwargs.get("customer_pk")
returns None
(or some other value which isn't a customer primary key); andget_object_or_404
returns a 404 errorYou probably just want to have the following:
def get_queryset(self):
return CustomerCommunication.objects.filter(customer_id= self.kwargs.get("customer_pk"))
I'm pretty sure this isn't an issue in django-rest-swagger
through.
I've already tried that but it returns an empty list when the customer does not exist which is not what I want. I implemented the list method to get the desired results but the code is not nearly as sexy.
The endpoints are not visible in swagger but they still work and I can do CRUD requests to those endpoints. I'm using an GenericViewset and it seems that the cause is the queryset sending a get_object_or_404 object to the filter query.
serializer_class = CustomerCommunicationSerializer
def get_queryset(self):
customer = get_object_or_404(Customer, pk=self.kwargs.get("customer_pk"))
return CustomerCommunication.objects.filter(customer=customer)
Changing the code above to the code below causes the endpoints to show up in swagger
serializer_class = CustomerCommunicationSerializer
queryset = CustomerCommunication.objects.all()