多态模型序列化器

时间:2019-02-22 06:09:03

标签: django django-rest-framework django-polymorphic

我正在使用Polymorphic模型来设置通知:

我的模特:

class Notification(PolymorphicModel):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    created_by = models.ForeignKey(ElsUser, on_delete=models.CASCADE, default=None, related_name="creatednotifications")
    created_on = models.DateTimeField(default=timezone.now)
    created_for = models.ForeignKey(ElsUser, on_delete=models.CASCADE, default=None, related_name="receivednotifications")
    read = models.DateTimeField(default=None, null=True, blank=True)
    message = models.CharField(default=None, blank=True, null=True, max_length=800)

    @property
    def total(self):
        return self.objects.filter(created_for=self.request.user).count()

    @property
    def unread(self):
        return self.objects.filter(created_for=self.request.user,read=None).count()

    @property
    def read(self):
        return self.objects.filter(created_for=self.request.user).exclude(read=None).count()    

class WorkflowNotification(Notification):
    # permission_transition = models.ForeignKey(WorkflowStatePermissionTransition, on_delete=models.CASCADE)
    action = models.ForeignKey(UserAction, on_delete=models.CASCADE)

目前,我只有一个模型从多态模型继承而来,但将来会有很多。

我正在尝试获取API中已登录用户的通知计数(总数)。.total作为属性字段给出,以提供帮助

我的序列化器:

class NotificationSerializer(serializers.ModelSerializer):
    total = serializers.ReadOnlyField()
    read = serializers.ReadOnlyField()
    unread = serializers.ReadOnlyField()

    class Meta:
        model = Notification
        fields = ['id', 'total','read', 'unread']

在视图中:

   class NotificationsMeta(generics.ListAPIView):
    serializer_class = NotificationSerializer
    queryset = Notification.objects.all()

当我尝试运行服务器时,它显示:

  Got AttributeError when attempting to get a value for field `total` on serializer `NotificationSerializer`.
The serializer field might be named incorrectly and not match any attribute or key on the `WorkflowNotification` instance.
Original exception text was: Manager isn't accessible via WorkflowNotification instances.

2 个答案:

答案 0 :(得分:2)

由于只需要“元数据”,制作模型序列化器有什么用?还是任何序列化器?序列化器将为您提供模型对象的序列化实例。因此,如果您有多个对象,则会得到多个序列化的对象作为响应。

只需将您的视图设为普通的APIView。因为没有必要序列化任何东西。

class NotificationsMeta(APIView):
    def get(self, request, format=None):
        qs = Notification.objects.filter(created_for=self.request.user)
        response = {
            'total': qs.count(),
            'read': qs.filter(read=None).count(),
            'unread': qs.exclude(read=None).count()
        }
        return Response(response)

现在从模型中删除那些属性函数。

我没有测试您的查询,只是从您的模型中复制了它们。您将需要检查它们是否正常工作。希望这会有所帮助。

答案 1 :(得分:0)

我不确定如何调用负责模型查询的模型属性如何从序列化程序中提供适当的数据。不幸的是,我确实对此有知识鸿沟。我正在考虑一种替代解决方案。我希望以下内容能起作用。

class NotificationSerializer(serializers.ModelSerializer):
    total = serializers.serializers.SerializerMethodField()

    read = serializers.ReadOnlyField()
    unread = serializers.ReadOnlyField()

    class Meta:
        model = Notification
        fields = ['read', 'unread']


    def get_total(self, obj):
        user =  self.context['request'].user 
        return Notification.objects.filter(created_for=user).count()

如果这项工作有效,那么您也可以做类似的事情来进行读取和未读取。

为了获取current_user的通知,我们需要从视图中覆盖get_queryset。

   class NotificationsMeta(generics.ListAPIView):
    serializer_class = NotificationSerializer
    def get_queryset(self):
       return Notification.objects.filter(created_for=self.request.user)