Django rest 3.1.1 - from one to many serializers with the "many" attribute

I want to create a simple serializer that anyone who wants to can add a Question with several answers (how much he needs)

one question, several answers

  • I want to be able to add using an assembly in html form and not edit json.

my models:

class Question(models.Model): question_text = models.CharField(max_length=30) class Answer(models.Model): question = models.ForeignKey(Question) answer_text = models.CharField(max_length=40) 

my url.py

 class AnswerSerializer(serializers.HyperlinkedModelSerializer): class Meta: model = Answer fields = ('answer_text',) class QuestionSerializer(serializers.HyperlinkedModelSerializer): answers = AnswerSerializer(many=True) class Meta: model = Question fields = ('question_text', 'answers',) class QuestionViewSet(viewsets.ModelViewSet): queryset = Question.objects.all() serializer_class = QuestionSerializer 

now when i start the web i get the message:

"Lists are not currently supported in HTML input."

please, help:)

edit first

even when I delete ("many = True"), I get an error when trying to send a message:

AssertionError at / questions / The .create() method does not by default support .create() nested fields. Write an explicit .create() method for serializer api_project2.urls.QuestionSerializer or set read_only=True on serializer nested fields.

thats creates m second problem: the create () method, which I do not know how to edit

+5
source share
2 answers

when you delete ("many = True"), you get an error when you try to publish because you are not rewriting the create function, you have to rewrite the create function

+3
source

Your quote answers your question. The built-in input forms of the HTML view do not support lists.

Support seems to have been planned for 3.1 , but I don't see the release notes mentioned in 3.1 .

+1
source

All Articles