Это мой код:
Models.py
class Kitchen(models.Model):
kitchen_name = models.CharField(max_length = 100)
state = models.CharField(max_length = 40, )
city = models.CharField(max_length = 40)
def __str__(self):
return self.kitchen_name
class AssignAudit(models.Model):
kitchen = models.ForeignKey(Kitchen, on_delete = models.SET_NULL, null = True)
assigned_date = models.DateTimeField(default = timezone.now)
assigned_by = models.ForeignKey(User, on_delete = models.SET_NULL, null = True, related_name = 'assigned_audits')
class KitchenAudit(models.Model):
assign_audit = models.ForeignKey(AssignAudit, on_delete = models.SET_NULL, null = True)
question = models.ForeignKey(Question, on_delete = models.SET_NULL, null = True)
total_images = models.IntegerField(null = True, blank = True)
marks_scored = models.IntegerField(null=True,blank=True)
comment = models.CharField(max_length = 300, null = True, blank = True)
Serializers.py
class KitchenSerializer(serializers.ModelSerializer):
class Meta:
model = Kitchen
fields = '__all__'
class AssignAuditSerializer(serializers.ModelSerializer):
kitchen = KitchenSerializer()
class Meta:
model = AssignAudit
fields = '__all__'
def create(self, validated_data):
assign_audit = AssignAudit.objects.create(**validated_data)
questions = Question.objects.all()
for question in questions:
kitchen_audit = KitchenAudit.objects.create(question=question,assign_audit=assign_audit)
for images in range(question.no_of_images):
KitchenAuditImage.objects.create(kitchen_audit=kitchen_audit)
return assign_audit
Views.py
class AssignAuditSerializerListCreate(ListCreateAPIView):
serializer_class = AssignAuditSerializer
queryset = AssignAudit.objects.all()
def list(self, request, *args, **kwargs):
queryset = self.filter_queryset(self.get_queryset())
page = self.paginate_queryset(queryset)
if page is not None:
serializer = self.get_serializer(page, many=True)
return self.get_paginated_response(serializer.data)
serializer = self.get_serializer(queryset, many=True)
return Response({'audit_assign':serializer.data})
Я хочу объединить обе мои таблицы выше, чтобы отобразить их в одном API с помощью django-rest-framework.Метод GET работает нормально, но когда я делаю метод POST, он говорит, что я должен предоставить кухню.
{
"kitchen": [
"This field is required."
]
}