如何在Django tastype中调用Resource实例。?

2024-10-03 02:35:44 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我的Tastype代码片段。在

我有一个资源,在post_list方法中,Mysample的一个实例正在那里创建。在

我想调用一个Mysample实例的方法,请帮助我怎么做

请在我需要调用Mysample实例的方法的代码中找到注释

class MysampleResource(ModelResource):
    intfeild1 = fields.IntegerField('intfeild1_id', null=True)
    intfeild2 = fields.IntegerField('intfeild1_id')

    class Meta:
        always_return_data = True
        queryset = Mysample.objects.all()
        allowed_methods = ['get','post','put','delete',]
        authentication = SessionAuthentication()
        authorization = MysampleAuthorization()


    def post_list(self, request, **kwargs):

            result = super(MysampleResource, self).post_list(request, **kwargs)

            #here I want to call a method of Mysample Instance.
            return result

请帮帮我,我是乞丐,所以你能给我建议,哪种方法可以覆盖,我应该在哪里这样做。在


Tags: 实例方法代码selfidtruefieldsreturn
1条回答
网友
1楼 · 发布于 2024-10-03 02:35:44

您只需在资源中添加方法:

def test_method(self,param*):
        #Do your stuff
        return result

在post_列表中,您可以将其称为:

^{pr2}$

注意:方法声明包含2个参数,但在python中,“self”作为隐式参数传递,这样当您调用方法时,不会传递self对象。在

  • =可能不止一个参数,在这种情况下,使用“,”将它们分开。在

如果我们应用前面的所有概念,您的代码应该如下所示:

class MysampleResource(ModelResource):
    intfeild1 = fields.IntegerField('intfeild1_id', null=True)
    intfeild2 = fields.IntegerField('intfeild1_id')

    class Meta:
        always_return_data = True
        queryset = Mysample.objects.all()
        allowed_methods = ['get','post','put','delete',]
        authentication = SessionAuthentication()
        authorization = MysampleAuthorization()


        def post_list(self, request, **kwargs):

                result = super(MysampleResource, self).post_list(request, **kwargs)

                #Let's say that you want to pass resquest as your param to your method
                method_result=self.test_method(request)
                return result

         def test_method(self,request):
                #Do your stuff
                return result

相关问题 更多 >