0

I am having some problems and doubts about how to call my own API within my app.

So I have an API to which I can send data. What I want to do in a different view is calling this sent data so I can visualize it in a template.

First I was trying to call the API with the library requests inside of my view. Even though that works I am having problems with authentication. So I was thinking I could call my class based API view from my custom function based view.

But I don't know if that is possible, nor do I know if that is recommendable. I was also thinking that it might be better to do that with javascript? I don't know.... So my question is twofold:

a) What is the best practice to call an API view/get API data from my own app so that I can manipulate and visualize it

b) If this is good practice, how can I call my class based generic API view from my custom function based view?

Here is what I am trying so far:

my generic view

class BuildingGroupRetrieveAPIView(RetrieveAPIView):
    """Retrieve detail view API.

    Display all information on a single BuildingGroup object with a specific ID.

    """

    authentication_classes = [JSONWebTokenAuthentication, SessionAuthentication, BasicAuthentication]
    serializer_class = BuildingGroupSerializer
    queryset = BuildingGroup.objects.all()

my function based view with which I try to call it:

def visualize_buildings(request, id):
    returned_view = BuildingGroupRetrieveAPIView.as_view()
    return returned_view


my url

    path('data/<int:pk>/', BuildingGroupRetrieveAPIView.as_view(),
         name="detail_buildings_api"),

When I call my class based view I get AttributeError: 'function' object has no attribute 'get'

Help is very much appreciated! Thanks in advance!

7
  • There's certainly no point making an actual HTTP request to get your data, but I can't understand what you are doing in visualize_buildings. You seem to be just calling the API view and returning it; what's the point? Why don't you remove that function and just go to the BuildingGroupRetrieveAPIView URL directly? Commented Aug 13, 2019 at 7:41
  • Well right now I am just trying to return it, you're right. But that doesn't work so no sense in writing more code. My idea was actually that when I manage to get the data from the api, I can then modify it and pass it modified to my template where I can then visualize it.... Commented Aug 13, 2019 at 7:45
  • 1
    OK but what "doesn't work"? What happens? Note that as_view() returns a view, ie a callable which you then have to call: return returned_view(request, pk). Commented Aug 13, 2019 at 7:46
  • Yes, my bad I should have added that (I updated my question). It tells me AttributeError: 'function' object has no attribute 'get'. But wait I'll try your suggestion Commented Aug 13, 2019 at 7:54
  • 1
    OK, you need a kwarg, so try return returned_view(request, pk=id) Commented Aug 13, 2019 at 8:05

2 Answers 2

1

What you can do if you want is to call your CBV after its declaration inside its file for the sake of easiness when declaring its URL.

views.py

class BuildingGroupRetrieveAPIView(RetrieveAPIView):
    .....


visualize_buildings = BuildingGroupRetrieveAPIView.as_view()

Then on your URLs, you use that name.

urls.py

from . import views


path('data/<int:pk>/', views.visualize_buildings, name="detail_buildings_api"),
Sign up to request clarification or add additional context in comments.

3 Comments

thank you very much Higor. But this is more for the sake of clarity I assume and is basically the same as calling it directly? I'll try it.
that throws me RecursionError: maximum recursion depth exceeded It was enough to do return returned_view(request, pk=id). But thanks a bunch!
It's weird that it throws that error. But you're right, it's for clarity. I've added a part I forgot but it shouldn't throw that error.
0

Correct way:

from django.url import reverse, resolve

def get_view_func(name_of_your_view):
    return resolve(reverse(name_of_your_view)).func

# if your are in a drf view:
get_view_func()(request._request)

# if you are in normal django view:
get_view_func()(request)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.