【问题标题】:Sum of model methods django模型方法之和 django
【发布时间】:2020-06-18 21:01:19
【问题描述】:

我正在尝试获取我的一些模型方法的总和并将它们显示在结果页面上。我不确定在我的views.py 中完成这项工作的可能性有多大。运行服务器时出现以下错误。

+“方法”和“方法”不支持的操作数类型

  1. models.py
class Organization(ModelFieldRequiredMixin, models.Model):
        exist = models.BooleanField(help_text='Does organization exist physically?')
        blacklist = models.BooleanField(help_text='Has organization previously been blacklisted by a national authority, funder or fund manager?')
        grant_amount = MoneyField(decimal_places=2, max_digits=12, help_text='Total amount of grant(s) from largest donor to organization ')
        estimatedAnnual_budget = models.IntegerField(help_text='Estimated annual budget of the organization (inclusive of the largest funder), in US Dollars')

class Scores(ModelFieldRequiredMixin, models.Model):
    organization = models.ForeignKey(Organization,on_delete=models.CASCADE) 
    score = models.DecimalField(max_digits=9, decimal_places=2)


    # Section1 - ORGANIZATIONAL BACKGROUND

    def exist_score(self):
        if self.organization.exist == True:
            self.score=0.1
            return self.score #The higher score

        else:
            score=0.1
            return self.score #The lower score

    def accessibility_score(self):
        if self.organization.accessibility == True:
            self.score=5
            return self.score

        else:
            score=0
            return self.score

    def blacklist_score(self):
        if self.organization.blacklist == True:
            self.score=0
            return self.score

        else:
            score=0
            return self.score

    # Section2 - PREVIOUS GRANTS & PERFORMANCE
    def grant_amount_score(self):
        if self.organization.grant_amount >= 2000000:
            self.score=3.5
            return self.score

        else:
            score=0
            return self.score

    def estimatedAnnual_budget_score(self):
        if self.organization.estimatedAnnual_budget == True:
            self.score=2
            return self.score

        else:
            score=0.1
            return self.score

  1. Views.py
def results_view(request):  
    scores=Scores()

    previous_implementation = scores.exist_score + scores.accessibility_score + scores.blacklist_score + scores.grant_amount_score + scores.estimatedAnnual_budget_score

【问题讨论】:

    标签: python django model


    【解决方案1】:

    你试过了吗?

    previous_implementation = scores.exist_score() + scores.accessibility_score() + scores.blacklist_score() + scores.grant_amount_score() + scores.estimatedAnnual_budget_score()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-10-13
      • 2013-04-26
      • 2011-08-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多