0

Im creating a serializer using python django DRF .Im creating a separate helper class and trying to override the create method.

My helper class:

class WorkFlowHelper:

    def assign_level_permissions(self,workflow_level,level):
        for permission in level['permissions']:
            workflow_permission_obj = WorkflowPermission.objects.get(short_name=permission['short_name'])
            workflow_level.permissions.add(workflow_permission_obj)

    def create_levels(self,workflow,levels):
        for level in levels:
            workflow_level = WorkflowLevel()
            workflow_level.workflow=workflow,
            workflow_level.level = level['level']
            workflow_level.operation=level['operation'] 
            workflow_level.save()
            workflow.levels.add(workflow_level)
            self.assign_level_permissions(workflow_level,level)

    def create_categories(self,workflow, categories):
       for category in categories:
           workflow_category_obj = WorkflowCategory.objects.get(short_name=category['short_name'])
           workflow.categories.add(workflow_category_obj)

    def create(self,name, description,tenant, levels, categories):
        workflow = Workflow.objects.create(name=name, description=description, tenant=tenant)
        self.create_levels(workflow,levels)
        self.create_categories(workflow,categories)
        workflow.save()

        return workflow

In my serializer i have to call the create method like :

def create(self, validated_data):
    name=validated_data['name']
    description=validated_data.get('description'),
    tenant=self.context['request'].user.tenant
    levels = self.initial_data['levels']
    categories = self.initial_data['categories']
    helper = WorkFlowHelper()
    helper.create(name,description,tenant,levels,categories)

In the helper class ,im calling other functions from a function inside the same class. Im having doubts on how to pass in the self in the function call.Now im able to save the model and everything works,but just after post,its showing create() did not return object instanace AssertionError..but it is saving the model and working perfectly

3
  • You're making this too hard. When you reference one method (function) from another using 'self.', that will already pass 'self' as the first parameter. So all the places you are passwing 'workflow' as the first parameter are wrong. I don't know if just taking those out gives you the logic you desire, but the calls will then be well formed. Commented Feb 20, 2019 at 7:53
  • Like: "self.create_categories(workflow,categories)" -> "self.create_categories(categories)" Commented Feb 20, 2019 at 7:54
  • ca you check my edited question? @JoeAdmin Commented Feb 20, 2019 at 8:25

2 Answers 2

0

self is a reference to object itself. Using self as first parameter of a function, you are able to create instance method. So if you want to use the instance method, then you need to initiate a object of that class, then call the function using that object. For example, in here:

helper = WorkFlowHelper()  # create a `WorkFlowHelper` object

helper.create(creates,name,description,tenant,levels,categories)
# here you don't need to pass `self`, because it is referencing to it to the helper object

For reference, you can check here.

Sign up to request clarification or add additional context in comments.

Comments

0

This worked:

class WorkFlowHelper:


    def assign_level_permissions(self,workflow_level,level):
        for permission in level['permissions']:
            workflow_permission_obj = WorkflowPermission.objects.get(short_name=permission['short_name'])
            workflow_level.permissions.add(workflow_permission_obj)

    def create_levels(self,workflow,levels):
        for level in levels:
            workflow_level = WorkflowLevel()
            workflow_level.workflow=workflow,
            workflow_level.level = level['level']
            workflow_level.operation=level['operation'] 
            workflow_level.save()
            workflow.levels.add(workflow_level)
            self.assign_level_permissions(workflow_level,level)

    def create_categories(self,workflow, categories):
       for category in categories:
           workflow_category_obj = WorkflowCategory.objects.get(short_name=category['short_name'])
           workflow.categories.add(workflow_category_obj)

    def create(self,name, description,tenant, levels, categories):
        workflow = Workflow.objects.create(name=name, description=description, tenant=tenant)
        self.create_levels(workflow,levels)
        self.create_categories(workflow,categories)
        workflow.save()

        return workflow

in serializer:

  def create(self, validated_data):
        name=validated_data['name']
        description=validated_data.get('description'),
        tenant=self.context['request'].user.tenant
        levels = self.initial_data['levels']
        categories = self.initial_data['categories']
        helper = WorkFlowHelper()
        workflow = helper.create(name,description,tenant,levels,categories)

        return workflow

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.