Reputation: 136
here is my models.py
class Post(models.Model):
author=models.ForeignKey(User,on_delete=models.CASCADE,related_name="post")
title=models.CharField(max_length=128,null=True,blank=True)
rate=models.IntegerField(validators=[MinValueValidator(1),MaxValueValidator(5)],default=True,null=True,blank=True)
# rating=models.IntegerField(null=True,blank=True)
content=models.TextField(null=True,blank=True)
review=models.CharField(max_length=250,null=True,blank=True)
url=models.URLField(null=True,blank=True)
voters = models.ManyToManyField(settings.AUTH_USER_MODEL,blank=True,related_name="post_voters")
tags = TaggableManager()
in my serializers.py i have imported
from taggit_serializer.serializers import (TagListSerializerField,
TaggitSerializer)
and here is the post serializer
class Spost(serializers.ModelSerializer,TaggitSerializer):
tags = TagListSerializerField()
author=serializers.StringRelatedField(read_only=True)
# likes_count=serializers.SerializerMethodField(read_only=True)
# user_has_voted=serializers.SerializerMethodField(read_only=True)
## for string related field without displaying it as numerics , it displays the direct object of that object"
# user=Scomments()
class Meta:
model = Post
fields = ('id','title','rate','author','content','review','url','tags')
def get_likes_count(self,instance):
return instance.voters.count()
def get_user_has_voted(self,instance):
request=self.context.get("request")
return instance.voters.filter(pk=request.user.pk).exists()
but what issue i have been faceing right now is whenever i trigger a post request with tags the object is being created im getting that the object along with tags being created but when i see in the admin panel the tags part isnt being updated
{
"rate": 4,
"content": "content",
"review": "dsfdf",
"url": "http://google.com",
"tags": [
"django",
"python"
]
}
this is the post request and in postman i can see the updated request
{
"id": 122,
"title": null,
"rate": 4,
"author": "User",
"content": "content",
"review": "dsfdf",
"url": "http://google.com",
"tags": [
"django",
"python"
]
}
but when i see the same thing in django admin panel and json list of all objects i can see that tag part is blank
{
"id": 122,
"title": null,
"rate": 4,
"author": "User",
"content": "content",
"review": "dsfdf",
"url": "http://google.com",
"tags": []
}
Upvotes: 0
Views: 1302
Reputation: 116
class Spost(TaggitSerializer, serializers.ModelSerializer):
tags = TagListSerializerField()
Here You have to pass TaggitSerializer
as the first argument in your serializer. As you are inheriting the TagListSerializerField
field from it.
Upvotes: 5