我的主要观点是我的视图返回一个空的json。我想要我的博客有各自的foreign_key user_id。
My models.py file
from django.db import models
# from django.contrib.auth.models import User
from django.utils import timezone
from datetime import datetime
from .validators import validate_allfile_extension
from restfapp.models import CustomUser
from django.contrib.auth.models import AbstractUser
CHOICE_GENDER = (('Male', 1), ('Female', 2), ('other', 3))
class CustomUser(AbstractUser):
gender = models.TextField(choices=CHOICE_GENDER, null=True)
birth_date = models.DateField(null=True, blank=True)
class Blog(models.Model):
title = models.TextField(max_length = 50)
author = models.TextField(max_length = 50)
description = models.TextField()
date = models.DateField(auto_now=True)
time = models.DateTimeField(default=datetime.now)
image = models.ImageField(null=True, verbose_name="", validators=[validate_allfile_extension])
user = models.ForeignKey(CustomUser, on_delete=models.CASCADE)
def __str__(self):
return self.title
我的序列化程序.py文件
class AddBlogSerializer(serializers.ModelSerializer):
title = serializers.CharField(max_length=128, write_only=True, required=True, error_messages={'blank': "Please provide title"} )
author = serializers.CharField(max_length=128, write_only=True, required=True, error_messages={'blank': "Please provide author"})
description = serializers.CharField(max_length=128, write_only=True, required=True, error_messages={'blank': "Please provide description"})
image = serializers.FileField(write_only=True, required=True, error_messages={'invalid': "Please upload image, video or audio file"})
class Meta:
model = Blog
fields = ["title","author","description","image",]
我的views.py文件
@api_view(['GET', 'POST'])
# @csrf_exempt
@authentication_classes([SessionAuthentication, BasicAuthentication])
@permission_classes([IsAuthenticated])
def blog(request):
if request.method == 'GET':
blogs_by_user_id = Blog.objects.filter(user_id=request.user.id).all()
serializer = AddBlogSerializer(blogs_by_user_id, many=True)
json_data = JSONRenderer().render(serializer.data)
return Response(json_data, status=200)
我的回应:
"[{},{},{},{},{}]"
它给了我空的json,我希望我的博客有各自的foreign_key user_id。
CustomUser
是您的AbstractBaseUser
吗?
因为request.user
将返回一个经过身份验证的AbstractBaseUser
。
顺便说一下,您可以使用request.user_id
而不是request.user.id
(您可以少做1个查询(。
对于你的view.py
,我会这样写:
- 使用
BlogSerializer
而不是AddBlogSerializer
- 只是
return serializer.data
不需要JSONRenderer()
,因为它是BlogSerializer
的工作
def blog(request):
if request.method == 'GET':
blogs = Blog.objects.filter(user_id=request.user_id)
serializer = BlogSerializer(blogs, many=True)
Response(serializer.data, status=status.HTTP_200_OK)
为我工作的最佳解决方案试试看:
write_only=True阻止序列化程序返回读取值。请考虑将它从您希望返回给客户端的任何字段中删除。
您的序列化程序应该是:
类AddBlogSerializer(序列化程序.ModelSerializer(:title=序列化程序。CharField(max_length=128,required=True,error_messages={'blank':"请提供标题"}(author=序列化程序。CharField(max_length=128,required=True,error_messages={'blank':"请提供作者"}(description=序列化程序。CharField(max_length=128,required=True,error_messages={'blank':"请提供描述"}(image=序列化程序。FileField(必需=True,error_messages={无效:请上传图像、视频或音频文件}(
class Meta:
model = Blog
fields = ["title","author","description","image",]
现在,它将返回您的json数据