Emulating an empty queryset with an abstract model

18 views Asked by At

I have a use case in which I have a ListAPIView that connects to a 3rd party (Stripe) API, fetches data (invoices) and returns that data to the user. I have a serializer, but I don't have a model.

The entire code looks something like this:

class InvoicesList(generics.ListAPIView):
  serializer_class = InvoiceSerializer

  def get_queryset(self):
    if getattr(self, 'swagger_fake_view', False):
      return   # <----  ¿?¿?¿?¿?¿?¿?¿?¿?
  
    return StripeWrapper().get_invoices()

class InvoiceSerializer(serializers.Serializer):
  ...fields..
  ...fields...
  ...fields

class StripeWrapper():
  def get_invoices():
   return requests.get(......)

Since I don't have a model, drf-spectacular refuses to generate the proper openapi specs. It expects to receive an EmptyQuerySet (SomeModel.objects.none()), but I can't provide it any since I don't have an Invoice model. I could create an abstract model like this:

class Invoice(models.Model):
  class Meta:
    abstract = True

but I still won't be able to provide drf-spectacular with an Invoice.objects.none() since there is no manager in that class (and there can't be since it's abstract).

How can I "emulate" (¿?) or "generate" an EmptyQuerySet so I can workaround this issue?

0

There are 0 answers