2

Im new to django When I make a api call it shows model.id but I wanted model.titleenter image description here

As we can see that my title is 1 but I wanted the title of the modelnot ID

Model.py

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)

views.py

def detail(request, id):
    posts = Post.objects.get(id=id)
    comments=posts.comment_set.all()
    forms=CommentForm
    if request.method == 'POST':
        form=CommentForm(request.POST)
        if form.is_valid():
            comment = form.save(commit=False)
            comment.title = posts
            print comment
            comment.save()
        else:
          print form.errors
    else:
        form = PostForm()

    return render(request, "detail_post.html", {'forms':forms,'posts': posts,'comments':comments})

serializer.py

class CommentSerializer(serializers.ModelSerializer):
    class Meta:
        model = Comment
        fields = ('title','comments')

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

How can I achieve the title of the blog name instead of just ID

Thanks in advance...

Coeus
  • 2,385
  • 3
  • 17
  • 27
  • You `Comment`s `title` property is a foreign key. `title=models.ForeignKey(Post)`. That's why your're getting a number. – Sevanteri Mar 17 '16 at 08:50
  • What you need to do is get the `Post` object and get it's `title`. – Sevanteri Mar 17 '16 at 08:51
  • `fields = ('title__title', 'comments')` – Burhan Khalid Mar 17 '16 at 08:54
  • @BurhanKhalid Does that fire another DB query or should a prefetch be perfomed before? – Sevanteri Mar 17 '16 at 08:56
  • @Coeus Well first you should change `Comment`s `title` to `post` so it's actually named after what it is. Then in the view when you get the comments with `posts.comment_set.all()` you should ensure that this prefetches the `Post` data too so you don't fire unneeded DB queries. I'll try to Google around a bit. Then do what @BurhanKhalid said. Put the 'title__title' ('post__title' after you change if you change.) in the comment seralizer. – Sevanteri Mar 17 '16 at 09:04
  • 1
    In the view you could get the comments like [this](http://stackoverflow.com/a/12974801/887828). This way you don't run unnecessary DB queries. – Sevanteri Mar 17 '16 at 09:10

2 Answers2

4
class CommentSerializer(serializers.ModelSerializer):

    title = serializers.CharField(source="title.title", read_only=True)

    class Meta:
        model = Comment
        fields = ('title','comments')
zxzak
  • 8,985
  • 4
  • 27
  • 25
3

If you want to get the post's title instead of the id from the CommentSerializer, you'll need to define explicitly the field in the CommentSerializer and use a SlugRelatedField.

Linovia
  • 19,812
  • 4
  • 47
  • 48