简体   繁体   English

Django Rest Framework序列化器的形式和嵌套关系

[英]Django Rest Framework serializers as forms and nested relationships

I am trying to use the django rest framework to generate html forms for model creation. 我正在尝试使用django rest框架生成用于模型创建的html表单。 Suppose I have a serializer that belongs to a model with a ManyToMany relation. 假设我有一个序列化程序,该序列化程序属于具有ManyToMany关系的模型。

class SerializerExample(serializers.ModelSerializer):
    mtm = ManyToManySerializer(many=True)

I then, in a django rest view, 然后,我在Django休息视图中

class AddModelView(StandardView):
    serializer_class = ModelSerializer
    renderer_classes = [TemplateHTMLRenderer]

    template_name = 'details.html'

    def get(self, request):
        model = Model.objects.get.all()
        serializer = ModelSerializer(model)
        return Response({'serializer': serializer, 'model': model})

And then suppose details.html looks like: 然后假设details.html看起来像:

{% load rest_framework %}

<html><body>

    <form method="POST">
        {% csrf_token %}
        {% render_form serializer %}
        <input type="submit" value="Save">
    </form>

    </body></html>

Lists are not currently supported in HTML input. instead of a multiselect or the abiliity to add new instances. 而不是多次选择或添加新实例的能力。 What am I doing wrong? 我究竟做错了什么?

You aren't missing anything. 您什么都不会错过。 This is work in progress. 这项工作正在进行中。 Either use JSON with nested serializers or don't use nested serializers if you need HTML Forms. 将JSON与嵌套的序列化程序一起使用,或者如果需要HTML表单,则不要使用嵌套的序列化程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM