Home > Software design >  'tuple' object has no attribute '_meta' while implementing serializer
'tuple' object has no attribute '_meta' while implementing serializer

Time:02-19

I am implementing serializers for my models. But I am getting this error while accessing the api endpoint saying "'tuple' object has no attribute '_meta'". Here are my models and serializers

Models

class WorkflowStep(models.Model):
    name = models.CharField(max_length=200)
    workflow = models.ForeignKey('WorkflowType', null=True, blank=True, related_name='workflow', on_delete=models.SET_NULL)

    allowed_statuses = models.ManyToManyField("WorkflowStepStatus", null=True, blank=True, related_name='status')
    active_status = models.ForeignKey('WorkflowStepStatus', null=True, blank=True,
                                      related_name='active_status', on_delete=models.SET_NULL)
    default_status = models.ForeignKey('WorkflowStepStatus', null=True, blank=True,
                                       related_name='default_status', on_delete=models.SET_NULL)

    should_recheck = models.BooleanField(null=True, blank=True)
    step_type = models.ForeignKey("WorkflowStepType", on_delete=models.SET_NULL, null=True,
                                  blank=True, related_name='step_type')
    trigger_on_new_version = models.BooleanField(null=True, blank=True)

    id = models.UUIDField(default=uuid.uuid4, editable=False, unique=True, primary_key=True)

    def __str__(self):
        return self.name   "_"   self.workflow.name


class Prerequisite(models.Model):
    workflow_step = models.ForeignKey("WorkflowStep", null=True, blank=True, on_delete=models.CASCADE,
                                      related_name='workflow_step')
    pre_requisite_step = models.ForeignKey('WorkflowStep', null=True, blank=True,
                                      on_delete=models.SET_NULL, related_name='pre_requisite_step')
    pre_requisite_step_status = models.ManyToManyField("WorkflowStepStatus",
                                             null=True,
                                             blank=True,
                                             related_name='pre_requisite_step_status')
    id = models.UUIDField(default=uuid.uuid4, editable=False, unique=True, primary_key=True)

    def __str__(self):
        return self.workflow_step.name   "_pre_req_"   self.workflow_step.workflow.name

Serializers

class WorkflowStepSerializer(serializers.ModelSerializer):
    workflow = WorkflowTypeSerializer(many=False)
    allowed_statuses = WorkflowStepStatusSerializer(many=True)
    active_status = WorkflowStepStatusSerializer(many=False)
    step_type = WorkflowStepTypeSerializer(many=False)
    prerequisite = serializers.SerializerMethodField()

    class Meta:
        model = WorkflowStep
        fields = '__all__'

    def get_prerequisite(self, obj):
        pre_requisites = obj.workflow_step.all()
        serializer = PrerequisiteSerializer(pre_requisites, many=True)
        return serializer.data

class PrerequisiteSerializer(serializers.ModelSerializer):
    workflow_step = WorkflowStepSerializer(many=True)
    pre_requisite_step = WorkflowStepSerializer(many=True)
    pre_requisite_step_status = WorkflowStepStatusSerializer(many=True)

    class Meta:
        model = Prerequisite,
        fields = "__all__"

Error

AttributeError at /api/workflowsteps/
'tuple' object has no attribute '_meta'
Request Method: GET
Request URL:    http://127.0.0.1:8000/api/workflowsteps/
Django Version: 3.2.9
Exception Type: AttributeError
Exception Value:    
'tuple' object has no attribute '_meta'
Exception Location: /Users/a/PycharmProjects/freshDjangoProject/venv/lib/python3.9/site-packages/rest_framework/utils/model_meta.py, line 35, in get_field_info
Python Executable:  /Users/a/PycharmProjects/freshDjangoProject/venv/bin/python
Python Version: 3.9.6
Python Path:    
['/Users/a/PycharmProjects/freshDjangoProject',
 '/usr/local/Cellar/[email protected]/3.9.6/Frameworks/Python.framework/Versions/3.9/lib/python39.zip',
 '/usr/local/Cellar/[email protected]/3.9.6/Frameworks/Python.framework/Versions/3.9/lib/python3.9',
 '/usr/local/Cellar/[email protected]/3.9.6/Frameworks/Python.framework/Versions/3.9/lib/python3.9/lib-dynload',
 '/Users/a/PycharmProjects/freshDjangoProject/venv/lib/python3.9/site-packages']
Server time:    Fri, 18 Feb 2022 13:11:19  0000

CodePudding user response:

class PrerequisiteSerializer(serializers.ModelSerializer):
    workflow_step = WorkflowStepSerializer(many=True)
    pre_requisite_step = WorkflowStepSerializer(many=True)
    pre_requisite_step_status = WorkflowStepStatusSerializer(many=True)

    class Meta:
        model = Prerequisite, <---- remove comma right there
        fields = "__all__"
  • Related