Я хочу создать приложение о посещаемости ученического класса в таком упрощенном формате, как этот.Я получаю реальный выходной ответ, что мне нужно в GET запрос, как это.
[
{
"id": 1,
"present": "Yes",
"schedule": {
"id": 2,
"schedule_date": "2019-02-28",
"start_time": "2019-02-28T11:00:00Z",
"end_time": "2019-02-28T11:45:00Z",
"description": "new schedule",
"class_name": 2,
"class_room": 1,
"subject": 2,
"teacher": 3
},
"student": {
"id": 2,
"username": "student",
"email": "",
"first_name": "rakhi",
"last_name": "sawant"
}
},
{
"id": 2,
"present": "No",
"schedule": {
"id": 2,
"schedule_date": "2019-02-28",
"start_time": "2019-02-28T11:00:00Z",
"end_time": "2019-02-28T11:45:00Z",
"description": "new schedule",
"class_name": 2,
"class_room": 1,
"subject": 2,
"teacher": 3
},
"student": {
"id": 5,
"username": "roshanritik",
"email": "ritik@gmail.com",
"first_name": "Ritik",
"last_name": "Roshan"
}
}
]
, но в случае запроса POST я не получаю то, что мне действительно нужно для формата.Я дал запрос ввода, как это
{
"present" : "Yes",
"student" : {
"id" : 1
},
"schedule" :{
"id" : 2
}
}
и хочет вывод POST такой же, как GET Response, как указано выше.
Это мой Model.py
class Schedule(models.Model):
schedule_date = models.DateField()
start_time = models.DateTimeField()
end_time = models.DateTimeField()
description = models.CharField(max_length=200)
class_name = models.ForeignKey(ClassName, on_delete=models.CASCADE)
class_room = models.ForeignKey(ClassRoom, on_delete=models.CASCADE)
subject = models.ForeignKey(Subject, on_delete=models.CASCADE)
teacher = models.ForeignKey(User, on_delete=models.CASCADE)
def __str__(self):
return self.description
# Student's Attendance Table
class Attendance(models.Model):
present = EnumField(choices=['Yes', 'No'])
student = models.ForeignKey(User, on_delete=models.CASCADE)
schedule = models.ForeignKey(Schedule, on_delete=models.CASCADE)
def __str__(self):
return self.present
class Meta:
unique_together = ('student', 'schedule')
serializer.py
class AttendanceSerializers(serializers.ModelSerializer):
schedule = ScheduleSerializers(many=False, required=False)
student = UserSerializer(many=False, required=False)
class Meta:
model = Attendance
fields = ('id', 'present', 'schedule', 'student')
def create(self, validated_data):
schedule_data = validated_data.pop('schedule')
schedule = SchedulePartialSerializers.create(SchedulePartialSerializers(), validated_data=schedule_data)
student_data = validated_data.pop('student')
student = UserPartialSerializers.create(UserPartialSerializers(), validated_data=student_data)
attendance, created = Attendance.objects.update_or_create(schedule=schedule, student=student,
present=validated_data.pop('present'))
return attendance
Views.py
class AttendanceView(generics.ListCreateAPIView):
queryset = Attendance.objects.all()
serializer_class = AttendanceSerializers
def create(self, request, *args, **kwargs):
write_serializer = AttendanceSerializers(data=request.data)
write_serializer.is_valid(raise_exception=True)
instance = self.perform_create(write_serializer)
read_serializer = AttendanceSerializers(instance)
return Response(read_serializer.data)
userserializer
class UserSerializers(serializers.ModelSerializer):
class Meta:
model = User
fields = ('id', 'first_name')
ScheduleSerializer
class ScheduleSerializers(serializers.ModelSerializer):
class Meta:
model = Schedule
fields = "__all__"
Пожалуйста, проверьте и проверьте код.Это будет полезно.