如何使用django rest framework

时间:2016-08-28 20:00:18

标签: python django django-rest-framework

我在模板中获得了选择字段的ID。但是我想要显示选择字段的名称。请查看以下文件,并告诉我如何获取名称。

这是我的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)
    posted=models.ForeignKey(Posted, blank=True)
    tags= models.ManyToManyField(Tags, blank=True)
    content = models.TextField()
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

serializers.py

class BlogsSerializer(serializers.ModelSerializer):
    author = AccountSerializer(read_only=True,required=False)
    class Meta:
        model = Blogs
        fields = ('id','author','title','tags','posted','content','created_at','updated_at')
        read_only_fields=('id','created_at','updated_at')

class TagsSerializer(serializers.ModelSerializer):
    name = serializers.SerializerMethodField()   
    class Meta:
        model = Tags 
        fields = ('id','name')
        read_only_fields=('id','name')
class PostedSerializer(serializers.ModelSerializer):
    name = serializers.SerializerMethodField()
    class Meta:
        model = Posted
        fields = ('id','name')
        read_only_fields=('id','name')

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)

class TagsViewSet(viewsets.ModelViewSet):
    queryset=Tags.objects.all
    serializer_class= TagsSerializer

class PostedViewSet(viewsets.ModelViewSet):
    queryset=Posted.objects.all
    serializer_class= PostedSerializer

模板



<div class="row">
  <div class="col-sm-12">
    <div class="well">
      <div class="blog">
        <div class="blog__meta">
          <a href="#">
            +{{ blog.author.first_name }}
          </a>
        </div>

        <div class="blog__content">
          <p>{{ blog.title }}</p>
          <p>{{ blog.posted}}</p>
          <p>{{ blog.tags}}</p>
          <p>{{ blog.content }}</p>

        </div>
      </div>
    </div>
  </div>
</div>
&#13;
&#13;
&#13;

提前致谢

3 个答案:

答案 0 :(得分:1)

您可以使用SlugRelatedField选项将特定字段作为关系对象返回。例如,

class BlogsSerializer(serializers.ModelSerializer):
author = AccountSerializer(read_only=True,required=False)
tags = serializers.SlugRelatedField(
    many=True,
    read_only=True,
    slug_field='name'
 )
class Meta:
    model = Blogs
    fields = ('id','author','title','tags','posted','content','created_at','updated_at')
    read_only_fields=('id','created_at','updated_at')

答案 1 :(得分:1)

感谢@ Karol Sztajerwald。 是BlogsSerializer必须这样做。

最后博客Serializer.py是

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <a data-target="#page_1" href="#">Home</a>
    <a data-target="#page_2" href="#" >About us</a>
    <a data-target="#page_3"  href="#">Services</a>
    <a data-target="#page_4"  href="#">Partners</a>
    <a data-target="#page_5" href="#" >Contact us</a>
    
    <div id="page_1" class=welcome_bye_holder>
      <h1 id="page_1">Welcome to Cyber Technician:</h1>
      <p1 id="page_1">An I.T. Technician and cyber security<br> for the         <br>Southport, North Sefton area.</p1>
    </div>
    <div id="page_2" class=welcome_bye_holder>
      <h1 id="page_2">PAGE 2</h1>
      <p1 id="page_2">I PAGE 2 <br /> I PAGE 2</p1>
    </div>
  <div id="page_3" class=welcome_bye_holder>
     <h1 id="page_3">PAGE 3</h1>
      <p1 id="page_3">I PAGE 3 <br /> I PAGE 3</p1>
    </div>
    <div id="page_4" class=welcome_bye_holder>
      <h1 id="page_4">PAGE 4</h1>
      <p1 id="page_4">I PAGE 4 <br /> I PAGE 4</p1>
    </div>
<div id="page_5" class=welcome_bye_holder>
      <h1 id="page_5">PAGE 5</h1>
      <p1 id="page_5">I PAGE 5 <br /> I PAGE 5</p1>
    </div>

模板是

class TagsSerializer(serializers.ModelSerializer):
    class Meta:
        model = Tags

        fields = ('id','name')
        read_only_fields=('id','name')

class PostedSerializer(serializers.ModelSerializer):
    class Meta:
        model = Posted

        fields = ('id','name')
        read_only_fields=('id','name')
class BlogsSerializer(serializers.ModelSerializer):
    author = AccountSerializer(read_only=True,required=False)
    tags=TagsSerializer(many=True)
    posted=PostedSerializer()
    class Meta:
        model = Blogs

        fields = ('id','author','title','tags','posted','content','created_at','updated_at')
        read_only_fields=('id','created_at','updated_at')

答案 2 :(得分:0)

你的选择意味着什么?我在Field kwargs中看不到choices。您可以通过以下方式设置“选择”字段: chairs = models.IntegerField(max_length=255, choices=CHOICES)你在哪里得到id而不是名字?