Python 需要在Django Rest框架中使用AnonymousUser登录

Python 需要在Django Rest框架中使用AnonymousUser登录,python,django,django-rest-framework,Python,Django,Django Rest Framework,我有一个viewsetapi,它可以获得用户的身份验证通知,但我得到了错误。请看一看 视图集: from api.notifications.models import Notification from rest_framework.generics import ( ListAPIView ) from api.notifications.serializers import ( NotificationListSerializer ) from api.pagination import

我有一个viewsetapi,它可以获得用户的身份验证通知,但我得到了错误。请看一看

视图集:

from api.notifications.models import Notification 
from rest_framework.generics import ( ListAPIView )
from api.notifications.serializers import ( NotificationListSerializer )
from api.pagination import NewFeedPagination
from rest_framework.permissions import AllowAny
from api.permissions import IsOwnerOrReadOnly

class NotificationListAPIView(ListAPIView):
    permission_classes = [AllowAny]
    serializer_class = NotificationListSerializer
    ordering_fields = 'date'

    def get_queryset(self, *args, **kwargs):
        queryset_list = Notification.objects.filter(to_user=self.request.user)
        return queryset_list
登录并访问URL时,它已成功。但它没有登录,出现了错误:
int()参数必须是字符串或数字,而不是“AnonymousUser”
。如何设置AnonymousUser,URL将进入登录页面:localhost:8000/admin

所有回溯错误: 回溯:

File "C:\Python27\lib\site-packages\django\core\handlers\exception.py" in inner
  41.             response = get_response(request)

File "C:\Python27\lib\site-packages\django\core\handlers\base.py" in _get_response
  187.                 response = self.process_exception_by_middleware(e, request)

File "C:\Python27\lib\site-packages\django\core\handlers\base.py" in _get_response
  185.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "C:\Python27\lib\site-packages\django\views\decorators\csrf.py" in wrapped_view
  58.         return view_func(*args, **kwargs)

File "C:\Python27\lib\site-packages\django\views\generic\base.py" in view
  68.             return self.dispatch(request, *args, **kwargs)

File "C:\Python27\lib\site-packages\rest_framework\views.py" in dispatch
  489.             response = self.handle_exception(exc)

File "C:\Python27\lib\site-packages\rest_framework\views.py" in handle_exception
  449.             self.raise_uncaught_exception(exc)

File "C:\Python27\lib\site-packages\rest_framework\views.py" in dispatch
  486.             response = handler(request, *args, **kwargs)

File "C:\Python27\lib\site-packages\rest_framework\generics.py" in get
  201.         return self.list(request, *args, **kwargs)

File "C:\Python27\lib\site-packages\rest_framework\mixins.py" in list
  40.         queryset = self.filter_queryset(self.get_queryset())

File "C:\Users\User\Desktop\FeedGit\backend\api\notifications\views.py" in get_queryset
  16.         queryset_list = Notification.objects.filter(to_user=self.request.user)

File "C:\Python27\lib\site-packages\django\db\models\manager.py" in manager_method
  85.                 return getattr(self.get_queryset(), name)(*args, **kwargs)

File "C:\Python27\lib\site-packages\django\db\models\query.py" in filter
  782.         return self._filter_or_exclude(False, *args, **kwargs)

File "C:\Python27\lib\site-packages\django\db\models\query.py" in _filter_or_exclude
  800.             clone.query.add_q(Q(*args, **kwargs))

File "C:\Python27\lib\site-packages\django\db\models\sql\query.py" in add_q
  1261.         clause, _ = self._add_q(q_object, self.used_aliases)

File "C:\Python27\lib\site-packages\django\db\models\sql\query.py" in _add_q
  1287.                     allow_joins=allow_joins, split_subq=split_subq,

File "C:\Python27\lib\site-packages\django\db\models\sql\query.py" in build_filter
  1217.             condition = lookup_class(lhs, value)

File "C:\Python27\lib\site-packages\django\db\models\lookups.py" in __init__
  24.         self.rhs = self.get_prep_lookup()

File "C:\Python27\lib\site-packages\django\db\models\fields\related_lookups.py" in get_prep_lookup
  112.                 self.rhs = target_field.get_prep_value(self.rhs)

File "C:\Python27\lib\site-packages\django\db\models\fields\__init__.py" in get_prep_value
  962.         return int(value)

Exception Type: TypeError at /api/v1/notifications/
Exception Value: int() argument must be a string or a number, not 'AnonymousUser'
正如你的错误所说:

Exception Value: int() argument must be a string or a number, not 'AnonymousUser'
这意味着要过滤
通知
您正在传递的是完整的用户对象,而不是仅传递id,因此:

改变这一行,这就行了

 16.         queryset_list = Notification.objects.filter(to_user=self.request.user.id)

看起来您尚未在设置中设置DRF。尝试以下操作(或根据需要选择设置)并查看其是否有效:

REST_FRAMEWORK = {
    # Use Django's standard `django.contrib.auth` permissions,
    # or allow read-only access for unauthenticated users.
    'DEFAULT_AUTHENTICATION_CLASSES': (
        'rest_framework.authentication.TokenAuthentication',
        'rest_framework.authentication.SessionAuthentication',
    ),
    'DEFAULT_PERMISSION_CLASSES': [
        'rest_framework.permissions.IsAuthenticated'
    ]
}

你能把整个回溯错误公布出来吗?比如从哪个文件中,从哪个行号得到这个错误?回溯完成,兄弟!