简体   繁体   中英

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. Suppose I have a serializer that belongs to a model with a ManyToMany relation.

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

I then, in a django rest view,

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:

{% 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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