0

I am calling a class view inside class.Call_view value is "InsertionOrder" but it does not work instead directly writing 'InsertionOrder' works.Value is dynamic so i cant directly write 'InsertionOrder'.How to do this?

Error shows: 'str' object has no attribute 'as_view' in django

class GenericDetailView(APIView):

    def put(self, request,pk, format=None):
        url_name = request.resolver_match.url_name
        call_view = url_name.split("_")[0].title().replace('-', '')
        return call_view.as_view()(request._request, format)        this does not
        # return InsertionOrder.as_view()(request._request, format)   this works
class InsertionOrder(APIView):
         #Code goes on
Pravin Mishra
  • 177
  • 1
  • 9
  • I don't see any *operation performed in your `GenericDetailView`* class. If so, you can use the redirect option, which seems a better option. – JPG Sep 03 '20 at 02:59
  • Why are you not using `return InsertionOrder.as_view()(request._request, format)` ? – JPG Sep 03 '20 at 03:03
  • @ArakkalAbu because the value is dynamic.It can be someother class instead of InsertionOrder. – Pravin Mishra Sep 03 '20 at 03:05
  • then better to look at this, [How to dynamically load a Python class](https://stackoverflow.com/questions/547829/how-to-dynamically-load-a-python-class) – JPG Sep 03 '20 at 03:07
  • Does this answer your question? [How to dynamically load a Python class](https://stackoverflow.com/questions/547829/how-to-dynamically-load-a-python-class) – JPG Sep 03 '20 at 03:08
  • @ArakkalAbu no this is different.I string value is dynamic which refers to classname. – Pravin Mishra Sep 03 '20 at 03:25
  • How does that differ from the suggested duplicate? inputs are in the string, views are also Python classes – JPG Sep 03 '20 at 03:29
  • @ArakkalAbu solved it by using : getattr(sys.modules[__name__], class_name) – Pravin Mishra Sep 03 '20 at 04:30

0 Answers0