1

Im trying to get all the todos that belongs to the user,

here is my todo model

from django.db import models
from django.contrib.auth.models import User

class Todo(models.Model):
    _id = models.AutoField(primary_key = True)
    title = models.CharField(max_length = 250)
    content = models.TextField()
    created_at = models.DateField()
    complete = models.BooleanField(default = False)
    owner = models.ForeignKey(User, related_name = 'todo', on_delete = models.CASCADE, null = True)

and here is my todo view set

from rest_framework import viewsets, permissions
from .models import Todo
from .serializers import TodoSerializer

class TodoViewSet(viewsets.ModelViewSet):
    permissions_classes = [
        permissions.IsAuthenticated
    ] 
    
    serializer_class = TodoSerializer

    def query(self):
        return self.request.user.todo.all()

    def create(self, serializer):
        serializer.save(owner = self.request.user)

and when Im sending a req to /api/todo/ (the todos rest api) Im getting this error:

AttributeError: 'AnonymousUser' object has no attribute 'todo'

what could be the problem? and should I create user model? at the moment I'm using django user.

1
  • Is the request.user a User object? Doesn't look like it. Yes, you have to create the User model. Commented Jun 23, 2020 at 13:17

3 Answers 3

2

what could be the problem?

The problem here is that you are not logged in. So why is the request getting through you ask? Shouldn't it be stopped since the user has to be logged in? Problem behind the problem is a typo: permissions_classes should be permission_classes.

Now you will get a different error. But once you login everything should work :) So, no. No need to create a custom user model.

When you are not logged in django rest framework sets request.user to an AnonymousUser which is different to the normal django User. This is the reason why request.user doesn't have a todo attribute.

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

1 Comment

Think you mean "when you are not logged in" reg. AnonymousUser in DRF
1

The issue is that the user is not loggedin, Make sure you have an active user in your database and login. Then update your code with filter in your queryset.

You need to use FILTER in your queryset in your modelviewset checkout the working code below.

from rest_framework import viewsets, permissions
from .models import Todo
from .serializers import TodoSerializer

class TodoViewSet(viewsets.ModelViewSet):
    permission_classes = (permissions.IsAuthenticated,)

    serializer_class = TodoSerializer
    queryset = Todo.objects.all()


    def get_queryset(self):
        # after get all Todo then filtere by its owner and return the queryset
        todo = self.queryset.filter(owner=self.request.user)
        return todo

 
     # Also update the create method by overriding the perform_create method as below
     def perform_create(self, serializer_class):
         # todo is saved with the corresponding owner
        serializer_class.save(owner=self.request.user)

Comments

0

This is my created permission:

class IsManager(BasePermission):
    """
    Allows access only to Managers
    """
    def has_permission(self, request, view):
        return request.user.profession == 'M'

And it was returning:
AttributeError: 'AnonymousUser' object has no attribute 'todo'

  1. First of all there should be:

    permission_classes = [IsAuthenticated]

  2. I had same problem when was writing my own permissions.The main problem was i added field which was not in AnonymousUser (not authenticated user) so Django could not find any field in AnonymousUser (not authenticated user). That's why i added in views.py:

    permission_classes = [(IsAuthenticated & IsManager)]

and everything worked clearly. Hope this comment will help you.

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.