HTML输入当前不支持列表 [英] Lists are not currently supported in HTML input

查看:250
本文介绍了HTML输入当前不支持列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我的API端点使用Django REST通用视图。我的序列化器中的字段之一具有ManyToMany关系。我想将该字段显示到我的API端点中,但是出现此错误

I am using Django REST generic views for my API endpoint. One of the field in my serializer has ManyToMany relationship. I want to show that field into my API endpoint, But getting this error


HTML输入当前不支持列表。

Lists are not currently supported in HTML input.

我的看法是这样:

class AlertCreateView(ListCreateAPIView):
    permission_classes = (IsAuthenticated,)
    pagination_class = None
    serializer_class = AlertSerializer

    def get_queryset(self):
        queues = Queue.objects.all()
        for queue in queues:
           queryset = Alert.objects.filter(
               queue=queue
           )

        return queryset

我的序列化器是这样的:

My Serializer is this:

class AlertSerializer(serializers.ModelSerializer):
     queue = QueueSerializer(many=True)

     class Meta:
         model = Alert
         fields = (
             'id', 'name', 'queue','email', 'expected_qos'
         )


推荐答案

您不需要 get_queryset 方法,您可以执行以下操作:

You do not need the get_queryset method you could do something like this:

#views.py
class AlertCreateView(ListCreateAPIView):
     queryset = Alert.objects.all()
     serializer_class = AlertSerializer
     permission_classes = (IsAuthenticated,)

在序列化器中为 queues 字段命名,方法与在 related_name 的模型。并且您的 QueueSerializer 可以从 PrimaryKeyRelatedField 继承来呈现。

Name the queues field in the serializer in the same way as it is written in therelated_name of the model. And your QueueSerializer can inherit fromPrimaryKeyRelatedField to be rendered.

#models.py
class AlertModel(models.Model):
    ...
    queues = models.ManyToManyField(Queue, ... related_name='queues')     
    ...

#serializer.py
class QueueSerializer(PrimaryKeyRelatedField, serializers.ModelSerializer):
    class Meta:
       model: Queue

class AlertSerializer(serializers.ModelSerializer):
    queues = QueueSerializer(many=True, queryset=Queue.objects.all())

    class Meta:
        model = Alert
        fields = (
         'id', 'name', 'queues','email', 'expected_qos'
        )

这篇关于HTML输入当前不支持列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆