Good afternoon,
I’m having trouble posting a list of objects to the api.
I’m getting “serializer” has no is_valid attribute.
Model
class Record(models.Model):
tag_id = models.CharField(max_length= 255)
serial_number = models.CharField(max_length= 255)
is_read = models.BooleanField(default=False)
created_at = models.DateTimeField(auto_now_add=True)
Serializer
class RecordSerializer(serializers.Serializer):
id = serializers.IntegerField(read_only=True)
tag_id = serializers.CharField(required=True, allow_blank=False, max_length=100)
is_read = serializers.BooleanField(required=True)
created_at = serializers.DateTimeField(required=True)
serial_number = serializers.CharField(required=True, allow_blank=False, max_length=100)
synced = serializers.BooleanField(required=True)
def create(self, validated_data):
"""
Create and return a new `Record` instance, given the validated data.
"""
return Record.objects.create(**validated_data)
class Meta:
model = Record
fields = ('__all__')
View
class RecordSerializer(APIView):
def get(self, request, *args, **kwargs):
queryset = Record.objects.all()
serializers = RecordSerializer(queryset, many=True)
return Response({'status': 'success', "students":serializers.data}, status=200)
def post(self, request):
serializer = RecordSerializer(data=request.data)
print("SERIALIZER-----------------------")
print(serializer.data)
if serializer.is_valid(raise_exception = True):
serializer.save()
return Response({"status": "success", "data": serializer.data}, status=status.HTTP_200_OK)
else:
return Response({"status": "error", "data": serializer.errors}, status=status.HTTP_400_BAD_REQUEST)
I´m posting this by postman
[{"tag_id": "634360386", "serial_numer": "0000000087247f01", "is_read": "True", "created_at": "2023-01-26 14:43:57.658000+00:00", "synced": "False"}, {"tag_id": "2740706003", "serial_numer": "0000000087247f01", "is_read": "True", "created_at": "2023-01-26 14:44:14.789792+00:00", "synced": "False"}]
Thanks