type object 'X' has no attribute 'objects'

前端 未结 3 2039
情书的邮戳
情书的邮戳 2020-12-18 18:11

I am using Django and Django Rest Framework 2.4.0

I get the Attribute error type object \'Notification\' has no attribute \'objects\'

mo

3条回答
  •  暖寄归人
    2020-12-18 18:31

    The line notifications = Notification.objects.all() is referencing the Notification class defined in api.py and not models.py.

    The easiest way to fix this error is to rename the Notification class in either api.py or models.py so that you can refer to your model properly. Another option would be to use named imports:

    from .models import Notification as NotificationModel
    
    class Notification(generics.ListAPIView):
        ...
        def get_queryset(self):
            notifications = NotificationModel.objects.all()
            ...
    

提交回复
热议问题