In order to manage the data of the application, the admin interface and an API point can be set. From the admin panel we can see both the movie's data, and the user registered, writing the following admin.py
file:
from django.contrib import admin from books_recsys_app.models import MovieData,UserProfile class MoviesAdmin(admin.ModelAdmin): list_display = ['title', 'description'] admin.site.register(UserProfile) admin.site.register(MovieData,MoviesAdmin)
After setting the corresponding admin
URL on the urls.py
file:
url(r'^admin/', include(admin.site.urls))
We should see our admin panel (at http://localhost:8000/admin/
) with the two models and the data within the models resembles the fields specified in the admin.py
file:
To set the API endpoint to retrieve the data for each registered user, first we need to write out serializers.py
specifying which fields of the UserProfile
model we want to employ:
from books_recsys_app.models import UserProfile from rest_framework import serializers class UsersSerializer(serializers.HyperlinkedModelSerializer): class Meta: model = UserProfile fields = ('name', 'arrayratedmoviesindxs','lastrecs')
In this case, we want to collect the ID of the movies, rated by the user, and his last recommended movie's ID. Then the API is set in the api.py
file as follows:
from rest_framework import generics from rest_framework.permissions import AllowAny from rest_framework.pagination import PageNumberPagination from books_recsys_app.serializers import UsersSerializer from books_recsys_app.models import UserProfile class LargeResultsSetPagination(PageNumberPagination): page_size = 1000 page_size_query_param = 'page_size' max_page_size = 10000 class UsersList(generics.ListAPIView): serializer_class = UsersSerializer permission_classes = (AllowAny,) pagination_class = LargeResultsSetPagination def get_queryset(self): query = self.request.query_params.get if query('name'): return UserProfile.objects.filter(name=query('name')) else: return UserProfile.objects.all()
Note that a query parameter name
is allowed in case we want to collect only the data for one particular user. After setting the corresponding URL in the urls.py
file:
url(r'^users-list/',UsersList.as_view(),name='users-list')
The end point can be called through the curl
command using the terminal:
curl -X GET localhost:8000/users-list/
It can also be called using the swagger interface for testing purposes (see Chapter 6, Basics of Django: a simple web framework).
3.15.151.32