我有三个模型Blogs,Posted,Tags。在博客模型中,我有字段' postedin'作为发布模型的外键和标签'与标签模型一样多的领域。
models.py:
class Posted(models.Model):
name = models.CharField(_('Posted In'),max_length=255, unique=True)
class Tags(models.Model):
name = models.CharField(_('Tag Name'),max_length=255, unique=True)
class Blogs(models.Model):
author = models.ForeignKey(CustomUser)
title=models.CharField(max_length=100)
postedin=models.ForeignKey(Posted)
tags= models.ManyToManyField(Tags)
content = models.TextField()
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
views.py:
class BlogViewSet(viewsets.ModelViewSet):
queryset=Blogs.objects.order_by('-created_at')
serializer_class= BlogsSerializer
def get_permissions(self):
if self.request.method in permissions.SAFE_METHODS:
return (permissions.AllowAny(),)
return (permissions.IsAuthenticated(),IsAuthorOfBlog())
def perform_create(self,serializer):
serializer.save(author=self.request.user)
return super(BlogViewSet,self).perform_create(serializer)
serializers.py:
class TagsSerializer(serializers.ModelSerializer):
class Meta:
model = Tags
fields = ('pk','name')
read_only_fields=('pk','name')
class PostedSerializer(serializers.ModelSerializer):
class Meta:
model = Posted
fields = ('pk','name')
read_only_fields=('pk','name')
class BlogsSerializer(serializers.ModelSerializer):
author = AccountSerializer(read_only=True,required=False)
tags=TagsSerializer(read_only=True,many=True)
tags_id = serializers.PrimaryKeyRelatedField(queryset=Tags.objects.all(), write_only=True)
postedin = PostedSerializer(read_only=True)
postedin_id = serializers.PrimaryKeyRelatedField(queryset=Posted.objects.all(), write_only=True)
class Meta:
model = Blogs
fields = ('pk','author','title','tags','tags_id','postedin','postedin_id','content','created_at','updated_at')
read_only_fields=('pk','created_at','updated_at')
def get_validation_exclusions(self, *args, **kwargs):
exclusions = super(BlogsSerializer, self).get_validation_exclusions()
return exclusions + ['author']
def create(self, validated_data):
postedin = validated_data.pop('postedin_id')
tags = validated_data.pop('tags_id')
blogs = Blogs.objects.create(tags=tags,postedin=postedin, **validated_data)
return blogs
发送请求:
{title:" nvnbv",postedin_id:" 1",tags_id:[" 2"," 5",&#34 ; 1"," 4"],内容:" nmvmvjm"}
回复收到:
{tags_id:["不正确的类型。预期的pk值,收到的列表。"]}
我是Django-rest-framework的初学者。如何解决这个错误。
提前致谢!
答案 0 :(得分:2)
感谢@Abdulafaja的建议。
最后我得到了解决方案
BlogsSerializer应该是
class BlogsSerializer(serializers.ModelSerializer):
author = AccountSerializer(read_only=True,required=False)
tags=TagsSerializer(read_only=True,many=True)
tags_id = serializers.PrimaryKeyRelatedField(queryset=Tags.objects.all(), write_only=True,many=True)
postedin = PostedSerializer(read_only=True)
postedin_id = serializers.PrimaryKeyRelatedField(queryset=Posted.objects.all(), write_only=True)
class Meta:
model = Blogs
fields = ('pk','author','title','tags','tags_id','postedin','postedin_id','content','created_at','updated_at')
read_only_fields=('pk','created_at','updated_at')
def get_validation_exclusions(self, *args, **kwargs):
exclusions = super(BlogsSerializer, self).get_validation_exclusions()
return exclusions + ['author']
def create(self, validated_data):
postedin = validated_data.pop('postedin_id')
tags = validated_data.pop('tags_id')
blogs = Blogs.objects.create(postedin=postedin, **validated_data)
for tg in tags:
blogs.tags.add(tg)
return blogs