I am using ListSerializer for updating and creating list of objects, update()
works fine but cannot create list of objects (bulk_create).
models.py
class TutorUser(models.Model):
tutor_user = models.OneToOneField(settings.AUTH_USER_MODEL,
on_delete=models.CASCADE,
related_name='tutor')
full_name = models.CharField(max_length=255, blank=True)
phone_number = models.CharField(max_length=14, blank=True)
class WorkExperiance(models.Model):
tutor_work = models.ForeignKey(TutorUser,
related_name='tutor_work',
on_delete=models.CASCADE)
organization = models.CharField(max_length=255, blank=True)
start_year = models.IntegerField(null=True, blank=True)
serializers.py
class WorkExperianceListSerializer(serializers.ListSerializer):
def update(self, instance, validated_data):
tutor_work_mapping = {tutor_work.id: tutor_work for tutor_work in instance}
data_mapping = {item['id']: item for item in validated_data}
ret = []
for tutor_work_id, data in data_mapping.items():
print(tutor_work_id)
tutor_work = tutor_work_mapping.get(tutor_work_id, None)
# print(tutor_work_id)
if tutor_work is None:
ret.append(self.child.create(data))
# print(ret)
else:
ret.append(self.child.update(tutor_work, data))
for tutor_work_id, tutor_work in tutor_work_mapping.items():
if tutor_work_id not in data_mapping:
tutor_work.delete()
class WorkExperianceSerializer(serializers.ModelSerializer):
id = serializers.IntegerField(read_only=False)
class Meta:
list_serializer_class = WorkExperianceListSerializer
model = WorkExperiance
fields = [
'id',
'organization',
'start_year',
]
def update(self, instance, validated_data):
instance.organization = validated_data.get('organization', instance.organization)
instance.start_year = validated_data.get('start_year', instance.start_year)
instance.save()
return instance
views.py
class TutorWorkExperiance(APIView):
def get_object(self, request):
tutor = TutorUser.objects.get(tutor_user__id=request.user.id)
tutor_work = WorkExperiance.objects.filter(tutor_work=tutor)
return tutor_work
def put(self, request):
serializer = WorkExperianceSerializer(self.get_object(request), data = request.data, partial = True, many=True)
if serializer.is_valid(raise_exception=True):
serializer.save()
return Response(serializer.data)
return Response({"data": "Not valid"})
In my opinion the problem is here with ID, because WorkExperiance model foreign key with TutorUser model, and I cannot pass its ID when object is created. I am using only put method for both creating and updating.
Problem solved.
The issue is requesting post without ID, cause id = serializers.IntegerField(read_only=False)
defined in serializer, and also forgot to pass TutorUser Id which foreign key with WorkExperiance.
views.py:
def put(self, request):
tutor = TutorUser.objects.get(tutor_user__id=request.user.id)
serializer = WorkExperianceSerializer(self.get_object(request), data = request.data, partial = True, many=True)
if serializer.is_valid(raise_exception=True):
serializer.save(tutor_user=tutor)
return Response(serializer.data)
return Response({"data": "Not valid"})
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.