0

I've the following class based view in django rest,

class UserRoom(views.APIView):
    def add_user_to_persistent_room(self, request):
        try:
            user = User.objects.get(id=int(request.data['user_id']))
            club = Club.objects.get(id=int(request.data['club_id']))
            location = Location.objects.get(id=int(request.data['location_id']))
            name = location.city + '-' + club.name
            room, created = PersistentRoom.objects.get_or_create(name=name,
                                                                 defaults={'club': club, 'location': location})
            room.users.add(user)
            room.save()
            return Response(PersistentRoomSerializer(room).data, status=status.HTTP_201_CREATED)
        except User.DoesNotExist:
            return Response("{Error: Either User or Club does not exist}", status=status.HTTP_404_NOT_FOUND)


    def find_all_rooms_for_user(self, request, **kwargs):
        try:
            user = User.objects.get(id=int(kwargs.get('user_id')))
            persistent_rooms = user.persistentroom_set.all()
            floating_rooms = user.floatingroom_set.all()
            rooms = [PersistentRoomSerializer(persistent_room).data for persistent_room in persistent_rooms]
            for floating_room in floating_rooms:
                rooms.append(FloatingRoomSerializer(floating_room).data)
            return Response(rooms, status=status.HTTP_200_OK)
        except User.DoesNotExist:
            return Response("{Error: User does not exist}", status=status.HTTP_404_NOT_FOUND)

This is my urls.py

urlpatterns = [
    url(r'^rooms/persistent/(?P<user_id>[\w.-]+)/(?P<club_id>[\w.-]+)/(?P<location_id>[\w.-]+)/$',
        UserRoom.add_user_to_persistent_room(),
        name='add_user_to_persistent_room'),
    url(r'^rooms/all/(?P<user_id>[\w.-]+)/$', UserRoom.find_all_rooms_for_user(), name='find_all_rooms')
]

When I run this I get the following error,

TypeError: add_user_to_persistent_room() missing 2 required positional arguments: 'self' and 'request'

I understand the reason for this error clearly, my question is how do I pass the request object in the urls.py?

2
  • 1
    That isn't at all how you use class-base views. Commented May 21, 2018 at 17:25
  • @DanielRoseman are you suggesting I can't have private methods and will only have to call the class as view? Commented May 21, 2018 at 17:26

1 Answer 1

1

I think you used Class Based Views the wrong way. Your method must be named get or post. For example:

from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import authentication, permissions
from django.contrib.auth.models import User

class ListUsers(APIView):
    """
    View to list all users in the system.

    * Requires token authentication.
    * Only admin users are able to access this view.
    """
    authentication_classes = (authentication.TokenAuthentication,)
    permission_classes = (permissions.IsAdminUser,)

    def get(self, request, format=None):
        """
        Return a list of all users.
        """
        usernames = [user.username for user in User.objects.all()]
        return Response(usernames)

More info here: http://www.django-rest-framework.org/api-guide/views/#class-based-views

Sign up to request clarification or add additional context in comments.

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.