I want to create a simple serializer that everyone who want will be able to add a Question with multi Answers (how many that he want)
one Question- multi Answers
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 run the web I get the message:
"Lists are not currently supported in HTML input."
please help :)
first edit
even when i remove the ('many=True') i get an error while trying to post:
AssertionError at /questions/ The
.create()
method does not support writable nestedfields by default. Write an explicit.create()
method for serializerapi_project2.urls.QuestionSerializer
, or setread_only=True
on nested serializer fields.
thats creates m second problem: the create() method that i dont knwo how to edit
when you remove the ('many=True'), you get an error while trying to post because you have not rewrite the function create, you should rewrite the function create
Your quote answers your question. The built-in HTML view input forms do not support lists.
It seems that support was planned for 3.1 but I don't see any mention in the 3.1 release notes.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With