python - Django api returning model ID instead of model's title -


im new django when make api call shows model.id wanted model.titleenter image description here

as can see my title 1 wanted title of 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 achieve title of blog name instead of id

thanks in advance...

class commentserializer(serializers.modelserializer):      title = serializers.charfield(source="title.title", read_only=true)      class meta:         model = comment         fields = ('title','comments') 

Comments

Popular posts from this blog

java - Run spring boot application error: Cannot instantiate interface org.springframework.context.ApplicationListener -

reactjs - React router and this.props.children - how to pass state to this.props.children -

Excel VBA "Microsoft Windows Common Controls 6.0 (SP6)" Location Changes -