models.py
class Post(models.Model):
title = models.CharField(max_length=100)
text = models.CharField(max_length=200)
putdate = models.DateTimeField(auto_now_add=True)
tags = models.ForeignKey('Tags', on_delete=models.PROTECT)
def __str__(self):
return self.title
class Tags(models.Model):
name = models.CharField(max_length=255)
def __str__(self):
return self.name
selizator.py
class PostSerializer(serializers.ModelSerializer):
model = Post
fields = ['id', 'title', 'text', 'putdate', 'tags']
class TagsSerializer(serializers.ModelSerializer):
class Meta:
model = Tags
fields = '__all__'
views.py
class PostView(APIView):
def get(self, request):
output = [{"title": output.title,
"text": output.text,
"putdate": output.putdate,
"tags": output.tags }
for output in Post.objects.all()]
return Response(output)
def post(self, request):
serializer = PostSerializer(data=request.data)
if serializer.is_valid(raise_exception=True):
serializer.save()
return Response(serializer.data)
class TagViewSet(viewsets.ModelViewSet):
queryset = Tags.objects.all()
serializer_class = TagsSerializer
и сама ошибка
raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type Tags is not JSON serializable
[17/Jul/2023 23:43:37] "GET / HTTP/1.1" 500 106197