如何使用外键创建对象
How to create object with Foreign Key
首先,这是我的模型:
class Question(models.Model):
description = models.CharField(max_length=200)
analysis = models.CharField(max_length=50)
def __str__(self):
return self.description
class QuestionOption(models.Model):
question = models.ForeignKey(Question,related_name='options')
content = models.CharField(max_length=100)
isAnswer = models.BooleanField()
def __str__(self):
return self.question.description + " " + self.content
我的序列化程序:
class QuestionSerializer(ModelSerializer):
class Meta:
model = Question
fields = '__all__'
QuestionOption
的Serializer
一样
我的视图集:
class QuestionViewSet(ModelViewSet):
queryset = Question.objects.all()
serializer_class = QuestionRetriveSerilzer
我想要post一个Json数据,像这样:
{
"options": [
{
"content": "This is the first option",
"isAnswer": false
},
{
"content": "This is the second option",
"isAnswer": true
}
],
"description": "which one is true?",
"analysis": "It's esay"
}
我希望我的QuestionViewSet
可以自动为我创建一个Question
和两个QuestionOption
,当我post那个Json数据时,options
是空列表,所以我覆盖了 QuestionViewSet
的 create
方法,像这样:
def create(self, request, *args, **kwargs):
serializer = QuestionSerializer(data=request.data)
question = serializer.save()
for data in request.data['options']:
data['question'] = question.id
optionSeializer = OptionSerializer(data=data)
print optionSeializer.is_valid()
optionSeializer.save()
return Response(serializer.data,status=status.HTTP_200_OK)
这个方法可以,但是我想找一个更简单的方法来做,因为我必须重写update
和其他方法,这不是一件容易的事...
那么如何设计Serializers
和ViewSet
来自动创建对象和使用外键更新对象?
drf-writable-nested 可能有帮助。
首先,这是我的模型:
class Question(models.Model):
description = models.CharField(max_length=200)
analysis = models.CharField(max_length=50)
def __str__(self):
return self.description
class QuestionOption(models.Model):
question = models.ForeignKey(Question,related_name='options')
content = models.CharField(max_length=100)
isAnswer = models.BooleanField()
def __str__(self):
return self.question.description + " " + self.content
我的序列化程序:
class QuestionSerializer(ModelSerializer):
class Meta:
model = Question
fields = '__all__'
QuestionOption
的Serializer
一样
我的视图集:
class QuestionViewSet(ModelViewSet):
queryset = Question.objects.all()
serializer_class = QuestionRetriveSerilzer
我想要post一个Json数据,像这样:
{
"options": [
{
"content": "This is the first option",
"isAnswer": false
},
{
"content": "This is the second option",
"isAnswer": true
}
],
"description": "which one is true?",
"analysis": "It's esay"
}
我希望我的QuestionViewSet
可以自动为我创建一个Question
和两个QuestionOption
,当我post那个Json数据时,options
是空列表,所以我覆盖了 QuestionViewSet
的 create
方法,像这样:
def create(self, request, *args, **kwargs):
serializer = QuestionSerializer(data=request.data)
question = serializer.save()
for data in request.data['options']:
data['question'] = question.id
optionSeializer = OptionSerializer(data=data)
print optionSeializer.is_valid()
optionSeializer.save()
return Response(serializer.data,status=status.HTTP_200_OK)
这个方法可以,但是我想找一个更简单的方法来做,因为我必须重写update
和其他方法,这不是一件容易的事...
那么如何设计Serializers
和ViewSet
来自动创建对象和使用外键更新对象?
drf-writable-nested 可能有帮助。