سلام.دوستان چرا نام دسته بندی من در خروجی جیسون که با رست فریمورک جنگو ساختم نال برمیگرده
from django.db import models
from django.contrib.auth.models import User
from django.utils import timezone
# Create your models here.
class Category(models.Model):
parent = models.ForeignKey('self', default=None, null=True, blank=True, on_delete=models.CASCADE)
name = models.CharField(max_length=100)
image = models.ImageField(upload_to='category', null=True)
def __str__(self):
return self.name
class Post(models.Model):
author = models.ForeignKey(User, on_delete=models.CASCADE)
title = models.CharField(max_length=200)
body = models.TextField()
status = models.BooleanField(default=False)
image = models.ImageField(blank=True, default=True)
favorite = models.BooleanField(default=False)
published = models.DateTimeField(default=timezone.now)
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
category = models.ManyToManyField(Category,)
def __str__(self):
return self.title
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__"
from .models import Post, Category
from django.contrib.auth import get_user_model
from .serializers import PostSerializer, CategorySerializer, UserSerializer
from rest_framework.viewsets import ModelViewSet
from .permissions import IsStaffOrReadOnly, IsAuthorOrReadOnly, IsSuperUserOrStaffReadOnly
class UserView(ModelViewSet):
queryset = get_user_model().objects.all()
serializer_class = UserSerializer
permission_classes = (IsSuperUserOrStaffReadOnly,)
class CategoryView(ModelViewSet):
queryset = Category.objects.all()
serializer_class = CategorySerializer
def get_permissions(self):
if self.action in ['list', 'create']:
permission_classes = [IsStaffOrReadOnly]
else:
permission_classes = [IsStaffOrReadOnly, IsAuthorOrReadOnly]
return [permission() for permission in permission_classes]
class PostView(ModelViewSet):
queryset = Post.objects.all()
serializer_class = PostSerializer
filterset_fields = ['status', 'author__username']
search_fields = ['title', 'body', 'author__username']
ordering_fields = ['status', 'published']
ordering = ['-published']
def get_permissions(self):
if self.action in ['list', 'create']:
permission_classes = [IsStaffOrReadOnly]
else:
permission_classes = [IsStaffOrReadOnly, IsAuthorOrReadOnly]
return [permission() for permission in permission_classes]
سلام
دلیلش این هست که category در مدل Post از نوع ManyToMany هست. اگر میخواید اسم category های مربوطه رو بگیرید یک راهش این میتونه باشه:
class PostSerializer(serializers.ModelSerializer):
category = serializers.SerializerMethodField('category_names')
author = serializers.CharField(source='author.username', read_only=True)
class Meta:
model = Post
fields = '__all__'
def category_names(self, obj):
return obj.category.all().values_list('name', flat=True)
آیا مایل به ارسال نوتیفیکیشن و اخبار از طرف راکت هستید ؟