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.