如何在Django-rest-framework serializer中的关系模型中获得额外的列? [英] How to get an extra column in relational model in Django-rest-framework serializer?

查看:757
本文介绍了如何在Django-rest-framework serializer中的关系模型中获得额外的列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有类别文章模型,文章在我的序列化程序中有一个外键引用类别我可以在Category模型中获取名称列,因为_ code> _str __ 方法,但如何获取类别中的其他列?

I have Category and Article model,Article has a foreign key reference Category,in my serializer i can get the name column in Category model because of the __str__ method,but how can i get other columns in Category model

models.py:

models.py:

# blog category models
class Category(models.Model):
    #id = models.IntegerField(primary_key=True,help_text='primary key',auto_created=True)
    name = models.CharField(max_length=50,help_text='category name')
    description = models.TextField(default='',help_text='category description')
    coverimg = models.CharField(max_length=200,default='',help_text='category front cover image')
    covercolor = models.CharField(max_length=7,default='#ffffff',help_text='color for each category background')
    totalitems = models.IntegerField(default=0,help_text='total items for each category')
    createtime = models.DateTimeField(auto_now_add=True)
    modifytime = models.DateTimeField(auto_now=True)

    categories = models.Manager()

    class Meta:
        db_table = 'article_category'
    def __str__(self):
        return self.name

#blog article models
class Article(models.Model):
    STATUS = (
        (0,'on'),
        (1,'off')
    )
    #id = models.IntegerField(primary_key=True,help_text='primary key',auto_created=True)
    category = models.ForeignKey(Category,related_name='articles', help_text='foreigner key reference Category')
    #author = models.ForeignKey(myadmin.User, help_text='foreigner key reference myadmin User')
    title = models.CharField(max_length=100, help_text='article title')
    description = models.TextField(help_text='article brief description')
    content = models.TextField(help_text='article content')
    like = models.IntegerField(default=0,help_text='like numbers')
    secretcode = models.CharField(max_length=512,help_text='who has the code can scan')
    status = models.IntegerField(choices=STATUS,help_text='status of the article')
    createtime = models.DateTimeField(auto_now_add=True,help_text='time that first created')
    modifytime = models.DateTimeField(auto_now=True,help_text='time when modified')

    articles = models.Manager()

    def __str__(self):
        return self.title
    class Meta:
        db_table = 'article'

    def save(self, *args, **kwargs):
        if not self.id:
            Category.categories.filter(pk=self.category.pk).update(totalitems = F('totalitems')+1)
        super(Article,self).save(*args, **kwargs)

serializers.py:

serializers.py:

#   Article catalog
class ArticleCatalogSerializer(serializers.ModelSerializer):
    category = serializers.StringRelatedField()
    articletags = serializers.StringRelatedField(many=True)
    covercolor = serializers.StringRelatedField()
    class Meta:
        model = Article
        fields = ('id', 'title', 'category', 'articletags', 'description', 'like', 'createtime', 'covercolor')

covercolor = serializers.StringRelatedField()会导致错误:文章'对象没有属性'covercolor ,我更改为:

covercolor = serializers.StringRelatedField() would cause an error:Article' object has no attribute 'covercolor and i changed to this :

已更改serializers.py:

#   category serializer for ArticleCatalogSerializer for nested relationship
class CategoryNestedRelationshipSerializer(serializers.ModelSerializer):
    class Meta:
        model = Category
        fields = ('covercolor',)

#   Article catalog
class ArticleCatalogSerializer(serializers.ModelSerializer):
    category = serializers.StringRelatedField()
    articletags = serializers.StringRelatedField(many=True)
    covercolor = CategoryNestedRelationshipSerializer(read_only=True)
    class Meta:
        model = Article
        ields = ('id', 'title', 'category', 'articletags', 'description', 'like', 'createtime', 'covercolor')

发生错误:

Got AttributeError when attempting to get a value for field `covercolor` on serializer `ArticleCatalogSerializer`.
The serializer field might be named incorrectly and not match any attribute or key on the `Article` instance.
Original exception text was: 'Article' object has no attribute 'covercolor'.

如何实现?

推荐答案

在您的编辑中,将ArticleCatalogSerializer更改为

In your edit , Change your ArticleCatalogSerializer to

class ArticleCatalogSerializer(serializers.ModelSerializer):
    category = CategoryNestedRelationshipSerializer()
    class Meta:
        model = Article

您将获得此格式的输出

{
"id": 1,
"category": {
"covercolor": "#ffffff"
},
"title": "sa",
"description": "bhjghj",
"content": "sda",
"like": 0,
"secretcode": "77",
"status": 0,
"createtime": "2015-04-18T07:52:57.230110Z",
"modifytime": "2015-04-18T07:52:57.230135Z"
}

如果你想要任何其他类别的列,你可以在你的类别序列化器中包含这样的

If you want any other column of category , you can include in your category serializer like this

class CategoryNestedRelationshipSerializer(serializers.ModelSerializer):
    class Meta:
        model = Category
        fields = ('covercolor','name','description')

这篇关于如何在Django-rest-framework serializer中的关系模型中获得额外的列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆