Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 名称错误:名称';类别';未定义,如何在Django中以相同的名称访问它的类?_Python_Json_Django_Django Rest Framework_Django Serializer - Fatal编程技术网

Python 名称错误:名称';类别';未定义,如何在Django中以相同的名称访问它的类?

Python 名称错误:名称';类别';未定义,如何在Django中以相同的名称访问它的类?,python,json,django,django-rest-framework,django-serializer,Python,Json,Django,Django Rest Framework,Django Serializer,models.py文件,其中包含具有名称、描述父类\类别字段的类别模型 class Category(models.Model): """ Categories representation model """ name = models.CharField(max_length=50) description = models.TextField() parent_category = models.F

models.py文件,其中包含具有名称、描述父类\类别字段的类别模型

class Category(models.Model):
    """ Categories representation model """
    name = models.CharField(max_length=50)
    description = models.TextField()
    parent_category = models.ForeignKey('self', on_delete=models.SET_NULL, null=True, blank=True)
py文件,该文件包含类别模型序列化程序及其所有字段

class CategorySerializer(serializers.ModelSerializer):
    """ product categories model serializer """
    parent_category = CategorySerializer()
    class Meta:
        """ profile model serializer Meta class """
        model = Category
        fields = (
            'id',
            'name',
            'description',
            'parent_category'
        )
views.py文件,API视图,以获取所有具有所需用户身份验证的可用类别

class GetCategoriesView(APIView):
    """ product categories getting view """
    permission_classes = (IsAuthenticated,)

    def get(self, request, *args, **kwargs):
        """ get request method """
        categories = Category.objects.all()
        serializer = CategorySerializer(categories, many=True, context={'request':request})
        return Response(data=serializer.data, status=HTTP_200_OK)
expected result,Json结果,包含来自父类字段的递归数据

{
    name:'boy shoes',
    description:'boy shoes category description'
    parent_category:{
        name:'shoes',
        description:'shoes category description',
        parent_category:{
            name:'clothes',
            description:'clothes category description',
            parent_category: null
        }
    }
}
当我遇到错误时,我注意到我不能直接访问同一个类中的类

NameError: name 'CategorySerializer' is not defined

    
我怎样才能解决这个问题?我想你可以帮我解决这个问题

感谢您的关注:)


不能将同一类用作类变量。仅供参考

class-SomeClass:
x=SomeClass()#无法使用名称错误:未定义名称“SomeClass”
定义初始化(自):
x=SomeClass()#这没问题
所以你必须把你的序列化程序改成这样

class CategorySerializer(serializers.ModelSerializer):
“”“产品类别模型序列化程序”“”
类元:
“”“配置文件模型序列化程序元类”“”
型号=类别
字段=(
“id”,
“姓名”,
“说明”,
“父类”
)
def get_字段(自身):
fields=super()。获取_字段()
字段['parent_category']=CategorySerializer()
返回字段

您可以显示整个视图文件吗?为什么在
CategorySerializer
类中有行
parent\u category=CategorySerializer()
?由于描述中没有指定错误堆栈,我想这就是错误发生的地方。默认情况下,它将只呈现父类的id,如{name:'category',description:'category description',parent_category:3},因此如果我想将其自定义为json,我必须将其指定为序列化器类非常感谢,它可以工作!:)