简体   繁体   English

AttributeError: 'Serializer' object 在 django rest 框架中没有属性 'Meta'

[英]AttributeError: 'Serializer' object has no attribute 'Meta' in django rest framework

I am using serializers.Serializer instead of ModelSerializer which doesn't require Meta class but it keep saying object has no attribute Meta.我使用的是serializers.Serializer而不是ModelSerializer,它不需要Meta class,但它一直说object没有属性Meta。 Iam not sure what is the issue but when I run the localserver, the main page gives error saying api fetch error and in terminal it says AttributeError: 'Serializer' object has no attribute 'Meta'.我不确定是什么问题,但是当我运行本地服务器时,主页显示错误说 api 获取错误,并且在终端中显示 AttributeError: 'Serializer' object has no attribute 'Meta'。

My view:我的观点:

class ExampleView(viewsets.ModelViewSet):
    queryset = Example.objects.all().order_by('-created_at')
    serializer_class = ExampleSerializer
    serializer_action_classes = {
        'get_all_students_of_a_class': ExampleDetailSerializer,
    }
  
    def get_serializer_class(self):

        """
            returns a serializer class based on the action
            that has been defined.
        """
        try:
            return self.serializer_action_classes[self.action]
        except (KeyError, AttributeError):
            return super(ExampleView, self).get_serializer_class()

    def get_employee_instance(self):
       /........../
       return teacher

    def get_details_of_employee(self,request,pk=None):            
        id = pk
        person = Person.objects.get(employee__id=id)
        data = {
            "employee": self.get_employee_instance(),
            "person": person
        }
        serializer_class = self.get_serializer_class()
        serializer = serializer_class(data)
        return Response(serializer.data, status=status.HTTP_200_OK)

My serializer:我的序列化器:

class ExampleDetailSerializer(serializers.Serializer):
    employee = serializers.StringRelatedField()
    person = PersonSerializer()
    /................/

However it works and I can perform action if I go to localhost/admin and other api calls from localhost.但是,如果我从 go 到 localhost/admin 和其他 api 从 localhost 调用,它可以工作,我可以执行操作。

Add a Meta class with model = YourModel添加 Meta class 与model = YourModel

class ExampleDetailSerializer(serializers.Serializer):
    employee = serializers.StringRelatedField()
    person = PersonSerializer()

    class Meta:
        model = Example # model name
        fields = ('__all__')

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Django REST框架:AttributeError:Serializer对象没有属性'Meta' - Django REST framework: AttributeError: Serializer object has no attribute 'Meta' Django Rest Framework:AttributeError:'NoneType'对象没有属性'_meta'[for OneToOneField] - Django Rest Framework: AttributeError: 'NoneType' object has no attribute '_meta' [for OneToOneField] Django Rest Framework Serializer 不想接受返回的列表,并返回 AttributeError 'str' 对象没有属性 'x' - Django Rest Framework Serializer do not want to accept returned list, and returns AttributeError 'str' object has no attribute 'x' AttributeError 'tuple' 对象没有属性 'values' django rest 框架 - AttributeError 'tuple' object has no attribute 'values' django rest framework django rest framework上的AttributeError QuerySet'对象没有属性'users - AttributeError on django rest framework QuerySet' object has no attribute 'users Django REST 框架:AttributeError:'DeferredAttribute' object 没有属性'isoformat' - Django REST Framework: AttributeError: 'DeferredAttribute' object has no attribute 'isoformat' AttributeError: 'NoneType' object 在 Django Rest 框架中通过 PATCH 调用使用嵌套序列化器更新数据时没有属性 '_meta' - AttributeError: 'NoneType' object has no attribute '_meta' while updating data using nested serializers via PATCH call in Django Rest Framework Django Rest Framework'QuerySet'对象没有属性'_meta' - django rest framework 'QuerySet' object has no attribute '_meta' 未调用序列化程序 AttributeError: 'str' object 没有属性 '_meta' - serializer not being called AttributeError: 'str' object has no attribute '_meta' Django AttributeError:“模型”对象没有属性“ _meta” - Django AttributeError: 'Model' object has no attribute '_meta'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM