'Return the category value as null in Django (json)
Why does my category return null when I receive Jason output? After serializing and receiving the code as Jason, I receive the category name null. An example of the codes is shown below.
**my serializer**
from .models import Post, Category
from rest_framework import serializers
from django.contrib.auth import get_user_model
class CategorySerializer(serializers.ModelSerializer):
class Meta:
model = Category
fields = ["id", "name", "image"]
class AuthorSerializer(serializers.ModelSerializer):
class Meta:
model = get_user_model()
fields = ["id", "username", "email"]
class PostSerializer(serializers.ModelSerializer):
category = serializers.CharField(source="category.name",
read_only=True)
author = serializers.CharField(source="author.username",
read_only=True)
class Meta:
model = Post
fields = "__all__"
class UserSerializer(serializers.ModelSerializer):
class Meta:
model = get_user_model()
fields = "__all__"
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
