Попытка сериализации моих моделей.Я хочу, чтобы конечная точка отображала данные с соответствующими данными (ключ foregin).
, но я получил ошибку, и это
TypeError: 'Author' object is not iterable
Это мои классы сериализатора
from rest_framework import serializers
from . models import Author, Article, Category
class AuthorSerializers(serializers.ModelSerializer):
class Meta:
model = Author
fields = '__all__'
class CategorySerializers(serializers.ModelSerializer):
class Meta:
model = Category
fields = '__all__'
class ArticleSerializer(serializers.ModelSerializer):
author = serializers.StringRelatedField(many=True)
category = serializers.StringRelatedField(many=True)
class Meta:
model = Article
fields = '__all__'
и это мои модели
from django.db import models
from django.contrib.auth.models import User
class Author(models.Model):
name = models.ForeignKey(User, on_delete=models.CASCADE)
detail = models.TextField()
def __str__(self):
return self.name.username
class Category(models.Model):
name = models.CharField(max_length=100)
class Article(models.Model):
author = models.ForeignKey(Author, on_delete=models.CASCADE, related_name='author')
title = models.CharField(max_length=200)
body = models.TextField()
category = models.ForeignKey(Category, on_delete=models.CASCADE)
и это мои взгляды:
class ArticleListCreateGet(ListAPIView, CreateAPIView):
queryset = Article.objects.all()
serializer_class = ArticleSerializer
class ArticleSingle(RetrieveAPIView):
queryset = Article.objects.all()
serializer_class = ArticleSerializer
lookup_field = 'pk'
# This is delete/update method
class ArticleDeleteUpdate(DestroyAPIView, UpdateAPIView):
queryset = Article.objects.all()
serializer_class = ArticleSerializer
lookup_field = 'pk'
и это мой URL
path('api/v1/article', views.ArticleListCreateGet.as_view(), name='article'),
path('api/v1/article/<int:pk>/detail', views.ArticleSingle.as_view(), name='article-single'),
path('api/v1/article/<int:pk>', views.ArticleDeleteUpdate.as_view(), name='article-delete-update'),