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