[Answered ]-Is there a way to specify abstract functions which take in values for __init__

2👍

How about doing what the generic Django class-based-views do and have a model property that you need to set?

class BetterAPIView(APIView):
    model = None

    def __init__(self, *args, **kwargs):
        if self.model is None:
            raise SomeSortOfConfigurationException
        super(BetterAPIView, self).__init__(*args, **kwargs)

    def get_object(self, pk):
        try:
            return self.model.objects.get(pk=pk)
        catch:
            raise Http404

And now:

class BlogDetail(BetterAPIView):
    model = Post

    def get(self, request, pk, format=None):
        post = self.get_object(pk)

Of course, this is a rough example, you would want to make it more robust…

Leave a comment