[Answered ]-Reverse foreign key relation in django serializers

1👍

you need two classes in serializer.py

class Author_serializer(serializers.ModelSerializer):
     class Meta:
          model = Author
          fields = ["author_id", "author_name"]

and and second must be under Author_serializer

class saveArticleAndAuthor_serializer(serializers.ModelSerializer):
     class Meta:
          model = Article
          fields = ["author", "article_title", "content"]
     author = Author_serializer()

in views.py you use only saveArticleAndAuthor_serializer and it will serialize Author_serializer as "second level depth"

Leave a comment