Discussion:
Calling an API app from another app inside a Django Project
Daniel Breves
2018-09-12 20:25:04 UTC
Permalink
Hi, there!

I'm new in DRF and I created a Django project with two apps: one for a REST
API and one for the frontend. I've created the API and it's working good,
but now, I'd like to call the API from the frontend app.

My code:

views.py from api:

class UserViewSet(viewsets.ModelViewSet):
"""
API endpoint that allows users to be viewd or edited.
"""
queryset = User.objects.all().order_by('-date_joined')
serializer_class = UserSerializer

views.py from frontend:

def home(request):

user_view_set = UserViewSet.as_view({'get':'list'})
response = user_view_set(request)
users = response.data
return render(request, 'index.html', {"users":users})

Doing that I can obtain the data, but it's in a strange format. So, I think
that I'm doing something wrong. I'd like to obtain the JSON response.

Can someone please help me with that?

thank you very much!
--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-framework+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Daniel Breves
2018-09-12 20:45:24 UTC
Permalink
I've just realized that I'm receiving a list of OrderedDict and that's ok.
But am I doing it the right way?
Post by Daniel Breves
Hi, there!
I'm new in DRF and I created a Django project with two apps: one for a
REST API and one for the frontend. I've created the API and it's working
good, but now, I'd like to call the API from the frontend app.
"""
API endpoint that allows users to be viewd or edited.
"""
queryset = User.objects.all().order_by('-date_joined')
serializer_class = UserSerializer
user_view_set = UserViewSet.as_view({'get':'list'})
response = user_view_set(request)
users = response.data
return render(request, 'index.html', {"users":users})
Doing that I can obtain the data, but it's in a strange format. So, I
think that I'm doing something wrong. I'd like to obtain the JSON response.
Can someone please help me with that?
thank you very much!
--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-framework+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Jakob Damgaard Møller
2018-09-13 04:09:37 UTC
Permalink
Depends what you want to archive. If you just want the users, you could do:

users = User.objects.all()

Remember to import User model
Post by Daniel Breves
I've just realized that I'm receiving a list of OrderedDict and that's ok.
But am I doing it the right way?
Post by Daniel Breves
Hi, there!
I'm new in DRF and I created a Django project with two apps: one for a
REST API and one for the frontend. I've created the API and it's working
good, but now, I'd like to call the API from the frontend app.
"""
API endpoint that allows users to be viewd or edited.
"""
queryset = User.objects.all().order_by('-date_joined')
serializer_class = UserSerializer
user_view_set = UserViewSet.as_view({'get':'list'})
response = user_view_set(request)
users = response.data
return render(request, 'index.html', {"users":users})
Doing that I can obtain the data, but it's in a strange format. So, I
think that I'm doing something wrong. I'd like to obtain the JSON response.
Can someone please help me with that?
thank you very much!
--
You received this message because you are subscribed to the Google Groups
"Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-framework+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Daniel Breves
2018-09-13 11:51:07 UTC
Permalink
First of all, thank you!! So... in this case, is it a good practice to
create a class that talks to the models and that will be called for both
API and frontend? Otherwise, I'll repeat a lot of code.
Post by Jakob Damgaard Møller
users = User.objects.all()
Remember to import User model
Post by Daniel Breves
I've just realized that I'm receiving a list of OrderedDict and that's
ok. But am I doing it the right way?
Post by Daniel Breves
Hi, there!
I'm new in DRF and I created a Django project with two apps: one for a
REST API and one for the frontend. I've created the API and it's working
good, but now, I'd like to call the API from the frontend app.
"""
API endpoint that allows users to be viewd or edited.
"""
queryset = User.objects.all().order_by('-date_joined')
serializer_class = UserSerializer
user_view_set = UserViewSet.as_view({'get':'list'})
response = user_view_set(request)
users = response.data
return render(request, 'index.html', {"users":users})
Doing that I can obtain the data, but it's in a strange format. So, I
think that I'm doing something wrong. I'd like to obtain the JSON response.
Can someone please help me with that?
thank you very much!
--
You received this message because you are subscribed to the Google Groups
"Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups
"Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an
For more options, visit https://groups.google.com/d/optout.
--
Daniel Breves Ferreira
--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-framework+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...