0

I have a API that has repeated number of fields for same posts

enter image description here

Though comments belongs to same title, they are divided into different object. I wanted to make comments to be a list like "comments": ["hi1","hi2","hi3","hi4","ads"] not sure how to achieve this. Everything to be in single dictionary

My model

class Post(models.Model):
    title=models.CharField(max_length=200)
    description=models.TextField(max_length=10000)
    pub_date=models.DateTimeField(auto_now_add=True)


    def __unicode__(self):
        return self.title

    def description_as_list(self):
        return self.description.split('\n')

class Comment(models.Model):
    title=models.ForeignKey(Post)
    comments=models.CharField(max_length=200)

    def __unicode__(self):
        return '%s' % (self.title)

serializer

class CommentSerializer(serializers.ModelSerializer):
    id = serializers.CharField(source="title.id", read_only=True)
    title = serializers.CharField(source="title.title", read_only=True)

    class Meta:
        model = Comment
        fields = ('id','title','comments')


class PostSerializer(serializers.ModelSerializer):
    class Meta:
        model = Post
        fields = ('id','title','description','pub_date')

views.py

class CommentList(generics.ListCreateAPIView):
    queryset = Comment.objects.all()
    serializer_class = CommentSerializer

class CommentDetail(generics.RetrieveUpdateDestroyAPIView):
    queryset = Comment.objects.all()
    serializer_class = CommentSerializer

What could be done to make a list?

Any help is much appreciated..Thanks in advance

Coeus
  • 2,385
  • 3
  • 17
  • 27

1 Answers1

0

Because comments only make sense for a given post, I would suggest getting rid of the CommentList and instead simply add the comments as the list you want, on each post. You can easily do this by changing your PostSerializer:

class PostSerializer(serializers.ModelSerializer):
    comments = serializers.SerializerMethodField(read_only=True)
    class Meta:
        model = Post
        fields = ('id','title','description','pub_date', 'comments')

    def get_comments(self, obj):
        comment_list = obj.comment_set.all()
        data = []
        for comment in comment_list:
            data.append(comment.comments)
        return data

This should add a list of comments on every post.

Let me know if this wasn't exactly what you were looking for.

dkarchmer
  • 5,434
  • 4
  • 24
  • 37
  • You can get rid of read_only=True. That's default for SerializerMethodField I guess. – Rohit Jain Mar 18 '16 at 16:38
  • @davka...I think there is some problem with line ......data.append(comment.comments).............my IDE says it cannot look for comments – Coeus Mar 18 '16 at 16:44
  • comments should automatically be added by django when you use `title=models.ForeignKey(Post)` in your model. Your IDE may not understand django. Try changing the model to `title=models.ForeignKey(Post, related_name='comments')`. See http://stackoverflow.com/questions/2642613/what-is-related-name-used-for-in-django – dkarchmer Mar 18 '16 at 16:52
  • Edited my answer with comments_set, but I prefer to use related_name not to have to remember what Django uses – dkarchmer Mar 18 '16 at 16:54
  • @davka........thanks for helping me buddy..........no it didnot work...given ..... 'for comment in comments:'......what does comments have to loop.....you should declare it right?...I hope you are missing something........ – Coeus Mar 20 '16 at 19:33
  • Sorry, when I did the last edit, I missed one spot. It should iterate over the query with comments which I assigned to comment_list – dkarchmer Mar 20 '16 at 20:40