从 django-notifications-hq 序列化 NotificationQuerySet 不起作用
Serializing NotificationQuerySet from django-notifications-hq not working
所以,我正在尝试将通知模型添加到我的 API 使用 DRF (Django REST Framework) 制作的通知模型中,但我收到此错误:
AttributeError: 'NotificationQuerySet' object has no attribute 'recipient'
我正在尝试序列化一个 Django 应用程序模型,Notification。来自此应用:
https://github.com/django-notifications/django-notifications
我的 ViewSet class 是这样的:
class NotificationsViewSet(viewsets.ViewSet):
serializer_class = NotificationsSerializer
def list(self, request):
queryset = Notification.objects.all()
return Response(NotificationsSerializer(queryset).data)
这是我的序列化程序:
class NotificationsSerializer(serializers.ModelSerializer):
class Meta:
model = Notification
fields = ('recipient','description')
depth = 0
因此,当数据传递给序列化程序时,它变为 "Void" 或没有任何数据。
在 list 方法中做一些类似的事情:
print queryset[0]
returns 通常是一个通知对象。但是当将这个查询集传递给序列化程序时,似乎是空的,并且出现了 AttributeError。
另外,用控制台试过这个:
notifications = Notification.objects.all()
那个returns一个NotificationQuerySet对象(可迭代)。那我可以:
for noti in notifications:
print noti
这将输出每个通知的所有 unicode 方法。
对于每个通知实例,我还可以访问模型属性:
for noti in notifications:
print noti.recipient
而且效果很好。
为什么将其传递给序列化程序时不起作用?很奇怪...
使用查询集初始化序列化程序时需要传递 many=True
。 DRF 会假定您传递的是单个对象,如果您不告诉它您传递的是多个对象,它会尝试直接从中获取字段。
这是一个完整的实现,其中自述文件为 drf
urls.py
...
import notifications.urls
urlpatterns = [
...
path("inbox/notifications/", views.NotificationViewSet.as_view({'get': 'list'}), name='notifications'),
]
serializers.py
class UserSerializer(serializers.ModelSerializer):
class Meta:
model = get_user_model()
class NotificationSerializer(serializers.Serializer):
recipient = UserSerializer(read_only=True)
unread = serializers.BooleanField(read_only=True)
target = GenericNotificationRelatedField(read_only=True)
verb = serializers.CharField()
views.py
from notifications.models import Notification
from .serializers import NotificationSerializer
NotificationViewSet(viewsets.ViewSet):
serializer_class = NotificationSerializer
def list(self, request):
queryset = Notification.objects.all()
return Response(NotificationSerializer(queryset, many=True).data)
所以,我正在尝试将通知模型添加到我的 API 使用 DRF (Django REST Framework) 制作的通知模型中,但我收到此错误:
AttributeError: 'NotificationQuerySet' object has no attribute 'recipient'
我正在尝试序列化一个 Django 应用程序模型,Notification。来自此应用:
https://github.com/django-notifications/django-notifications
我的 ViewSet class 是这样的:
class NotificationsViewSet(viewsets.ViewSet):
serializer_class = NotificationsSerializer
def list(self, request):
queryset = Notification.objects.all()
return Response(NotificationsSerializer(queryset).data)
这是我的序列化程序:
class NotificationsSerializer(serializers.ModelSerializer):
class Meta:
model = Notification
fields = ('recipient','description')
depth = 0
因此,当数据传递给序列化程序时,它变为 "Void" 或没有任何数据。 在 list 方法中做一些类似的事情:
print queryset[0]
returns 通常是一个通知对象。但是当将这个查询集传递给序列化程序时,似乎是空的,并且出现了 AttributeError。
另外,用控制台试过这个:
notifications = Notification.objects.all()
那个returns一个NotificationQuerySet对象(可迭代)。那我可以:
for noti in notifications:
print noti
这将输出每个通知的所有 unicode 方法。 对于每个通知实例,我还可以访问模型属性:
for noti in notifications:
print noti.recipient
而且效果很好。
为什么将其传递给序列化程序时不起作用?很奇怪...
使用查询集初始化序列化程序时需要传递 many=True
。 DRF 会假定您传递的是单个对象,如果您不告诉它您传递的是多个对象,它会尝试直接从中获取字段。
这是一个完整的实现,其中自述文件为 drf
urls.py
...
import notifications.urls
urlpatterns = [
...
path("inbox/notifications/", views.NotificationViewSet.as_view({'get': 'list'}), name='notifications'),
]
serializers.py
class UserSerializer(serializers.ModelSerializer):
class Meta:
model = get_user_model()
class NotificationSerializer(serializers.Serializer):
recipient = UserSerializer(read_only=True)
unread = serializers.BooleanField(read_only=True)
target = GenericNotificationRelatedField(read_only=True)
verb = serializers.CharField()
views.py
from notifications.models import Notification
from .serializers import NotificationSerializer
NotificationViewSet(viewsets.ViewSet):
serializer_class = NotificationSerializer
def list(self, request):
queryset = Notification.objects.all()
return Response(NotificationSerializer(queryset, many=True).data)