嵌套序列化器(深度/级别3)

时间:2019-07-08 13:24:09

标签: django django-rest-framework

尝试使用django rest框架添加第3个嵌套序列化程序 如何在给定的代码中添加第3个嵌套实现-

models.py

class Category(models.Model):
    cate_id = models.AutoField(primary_key=True)
    cate_name = models.CharField(max_length=45)                    
    class Meta:
        managed = False
        db_table = 'category'

class SubCategory(models.Model):
    sub_cate_id = models.AutoField(primary_key=True)
    sub_cate_name = models.CharField(max_length=45)
    sub_fk = models.ForeignKey('Category', models.CASCADE, db_column='sub_fk')

    class Meta:
        managed = False
        db_table = 'sub_category'


class Products(models.Model):
    pro_id = models.AutoField(primary_key=True)
    pro_name = models.CharField(max_length=45)
    description = models.CharField(max_length=45, blank=True, null=True)
    price = models.IntegerField()
    quantity = models.IntegerField()
    pro_cate_fk = models.ForeignKey('Category', models.CASCADE, db_column='pro_cate_fk')
    pro_sub_fk = models.ForeignKey('SubCategory', models.CASCADE, db_column='pro_sub_fk')
    image = models.CharField(max_length=205)

    class Meta:
        managed = False
        db_table = 'products'
from rest_framework import serializers
from .models import Category,SubCategory,Products

class ProductsSerializer(serializers.ModelSerializer):
    # x= ChildTable.objects.all().values
    class Meta:
        model  = Products
        fields = ('pro_id','pro_name','description','price','quantity','image')

class SubCategorySerializer(ProductsSerializer):
    products_set = ProductsSerializer(many=True, read_only=True)

    class Meta:
        model  = SubCategory
        fields = ('sub_cate_name','sub_cate_id','products_set')

class CategorySerializer(SubCategorySerializer):
    subcategory_set = ProductsSerializer(many=True, read_only=True,)
    # pro_subcate_set = SubCategorySerializer(many=True, read_only=True)

    class Meta:
        model  = Category
        fields = ('cate_name','cate_id','subcategory_set')

尝试时遇到此错误-

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

是否可以将2个已连接的串行器连接到另一个串行器?

1 个答案:

答案 0 :(得分:0)

进行测试,看看它是否有效。

[kthreadd]
[ksoftirqd/0]
[kworker/0:0H]
[migration/0]
[rcu_bh]
[rcuob/0]
[rcuob/1]
[rcuob/2]
[rcuob/3]
[rcuob/4]
[rcuob/5]
[rcuob/6]
[rcuob/7]
[rcuob/8]
[rcuob/9]
[rcuob/10]
[rcuob/11]
[rcuob/12]
[rcuob/13]
[rcuob/14]
[rcuob/15]
[rcuob/16]
[rcuob/17]
[rcuob/18]
[rcuob/19]
[rcuob/20]
[rcuob/21]
[rcuob/22]
[rcuob/23]
[rcuob/24]
[rcuob/25]
[rcuob/26]
[rcuob/27]
[rcuob/28]
[rcuob/29]
[rcuob/30]
[rcuob/31]
[rcuob/32]
[rcuob/33]
[rcuob/34]
[rcuob/35]
[rcuob/36]
[rcuob/37]
[rcuob/38]
[rcuob/39]
php-fpm: pool www
php-fpm: pool www
php-fpm: pool www
[kworker/1:1]
php-fpm: pool www
php-fpm: pool www
php-fpm: pool www
php-fpm: pool www
php-fpm: pool www
php-fpm: pool www
php-fpm: pool www
php-fpm: pool www
php-fpm: pool www
php-fpm: pool www
php-fpm: pool www
php-fpm: pool www
php-fpm: pool www
top
[kworker/3:1]
[kworker/0:1]
[kworker/u480:2]
[kworker/3:3]
sshd: andi [priv]
sshd: andi@notty
/usr/libexec/openssh/sftp-server
[kworker/2:0]
[kworker/1:0]
php-fpm: pool www
php-fpm: pool www
php-fpm: pool www
php-fpm: pool www
php-fpm: pool www
php-fpm: pool www
php-fpm: pool www
php-fpm: pool www
[kworker/2:3]
[kworker/3:0]
[kworker/3:2]
sshd: andi [priv]
/usr/libexec/openssh/sftp-server
[kworker/1:2]
local -t unix
bounce -z -t unix -u
php-fpm: pool www
php-fpm: pool www
php-fpm: pool www
php-fpm: pool www
trivial-rewrite -n rewrite -t unix -u
smtpd -n smtp -t inet -u -o stress= -s 3
smtpd -n smtp -t inet -u -o stress= -s 3
grep --color=auto ^[SD]
cut -b 3-
/usr/sbin/lvmetad -f