【问题标题】:Calling a function that is in the same class - Django调用同一类中的函数 - Django
【发布时间】:2019-08-11 21:01:39
【问题描述】:

调用同一类中的函数 - Django

我很困惑。

我无法在我的项目中完成这项工作。

但是,它可以在单独的仅 python 文件中运行。

view.py

from .lib.rest import Rest

    class AssinaturaUpdate():
        ...
        def post(self, request, id):
            assinatura = Assinatura.objects.filter(id=id).first()
            form = FormAssinatura(request.POST)
            if form.is_valid():
                Rest.update.(self, data=form.cleaned_data, assinatura_id=assinatura.id)
            return redirect('assinatura_edit', id=id)

rest.py

class Rest():  

    def trocaPlano(self, assinatura_id):
        payload = {
            "id": assinatura_id
        }
        print(payload)

    def update(self, data, assinatura_id):

        self.trocaPlano(assinatura_id=assinatura_id)  

        headers = {"Content-Type": "application/json", "Accept": "application/json", "Authorization": TOKEN}
        r = requests.put(url='https://rest.com/subscriptions/'+assinatura_id, data=json.dumps(payload), headers=headers)
    ...

“AssinaturaUpdate”对象没有属性“trocaPlano”

【问题讨论】:

  • 在上面的调用中 self 赋值在哪里?可能应该在做t = Test(); t.update(..)
  • 调用 Test.update(self... 有效。只有 trocaPlano() 无效
  • selfTest.update(self,...) 中的值是多少?
  • Pycharm ID 标识您需要拥有的东西。但是,没有它也行不通。
  • 错误是显示“测试”对象没有属性“trocaPlano”

标签: python django python-3.x python-2.7


【解决方案1】:

当你访问一个对象方法时,你需要启动一个类的实例。因此,当访问trocaPlanoupdate 方法时(那些有self 作为方法的第一个参数,那些引用对象本身,有点像JS 中的thisRest 类,你需要启动Rest 类对象,例如使用 Rest()

你需要像这样更新你的代码:

# view

from .lib.rest import Rest
from django.shortcuts import get_object_or_404

class AssinaturaUpdate():
    ...
    def post(self, request, id):
        assinatura = get_object_or_404(Assinatura, id=id)
        form = FormAssinatura(request.POST)
        if form.is_valid():
            Rest().update(form.cleaned_data, id)
        return redirect('assinatura_edit', id=id)

 # rest

class Rest():  

    def trocaPlano(self, assinatura_id):
        payload = {
            "id": assinatura_id
        }
        print(payload)

    def update(self, data, assinatura_id):

        self.trocaPlano(assinatura_id)  

        headers = {"Content-Type": "application/json", "Accept": "application/json", "Authorization": TOKEN}  # <-- How this token comes here
        r = requests.put(url='https://rest.com/subscriptions/'+assinatura_id, data=json.dumps(payload), headers=headers)

【讨论】:

    【解决方案2】:

    无参数

    def calc_total_deduction(self):
        total_deduction = self.otherDeductions + self.calc_income_tax() + self.calc_total_pension()
        return total_deduction
    
    def calc_net_salary(self):
        net_salary = self.calc_gross_salary() - self.calc_total_deduction()
        return net_salary
    

    如果有参数

    def calc_total_deduction(self):
        total_deduction = self.otherDeductions + self.calc_income_tax() + self.calc_total_pension()
        return total_deduction
    
    def calc_net_salary(self):
        net_salary = self.calc_gross_salary(self.employee) - self.calc_total_deduction()
        return net_salary
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-15
      • 1970-01-01
      • 1970-01-01
      • 2019-04-18
      • 1970-01-01
      • 2011-05-02
      相关资源
      最近更新 更多