我使用python django DRF创建了一个序列化程序。我创建了一个单独的帮助器类并尝试覆盖create方法。
我的助手班:
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
在我的序列化程序中,我必须调用create方法,如:
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)
在helper类中,即从同一类内部的函数调用其他函数。我对如何在函数调用中传递self有疑问。现在我无法保存模型,并且一切正常,但是在发布之后,显示create()的实例并没有返回对象AssertionError ..但是它正在保存模型并完美运行
答案 0 :(得分:1)
self
是对对象本身的引用。使用self
作为函数的第一个参数,您可以创建实例方法。因此,如果要使用实例方法,则需要启动该类的对象,然后使用该对象调用该函数。例如,在这里:
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
作为参考,您可以勾选here。
答案 1 :(得分:0)
这有效:
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
在序列化器中:
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