在Django项目中。在视图中添加注释时,出现未定义项目的错误

问题描述 投票:0回答:1

我知道,这个问题已经被提及过几次了,但是我找不到解决问题的方法。因此,请接受我的歉意以再次请求。

我有一个基于条件的过滤器,不适用于我。

这是我的models.py文件:

class Itemslist(models.Model):
    safety_stock = models.DecimalField(blank=True, null=True, max_digits=19, decimal_places=0)
    current_stock = models.DecimalField(max_digits=19, decimal_places=0)

    def above_below_ss(self):
        ab = Decimal(self.current_stock-self.safety_stock)
        return round(ab,0)


    def __str__(self):
        return self.item_n

抱歉,必须纠正缩进,因为它们都属于一个模型类。

这是views.py文件中的内容:

from .models import *

def toorder(request):
    # toorder=Itemslist.objects.all
    sorted=Itemslist.objects.annotate(dontshow=above_below_ss()).exclude(dontshow__gt=0)
    context={ 'sorted': sorted }
    return render(request, 'toorder.html', context)

所以这是一个问题:当我使用

toorder=Itemslist.objects.all

一切正常,但是当我尝试这样做时:

sorted=Itemslist.objects.annotate(dontshow=above_below_ss()).exclude(dontshow__gt=0)

没有。

有趣的是,它曾经可以工作,但是我的代码在没有副本的情况下崩溃了((在备份期间,这很有趣),]]

现在不起作用。

我收到此消息:

NameError at /toorder
name 'above_below_ss' is not defined
Request Method: GET
Request URL:    http://localhost:8000/toorder
Django Version: 2.2.5
Exception Type: NameError
Exception Value:    
name 'above_below_ss' is not defined
Exception Location: /Users/artursjermolickis/projects/osmiocenter/mysite/itemlist/views.py in toorder, line 220
Python Executable:  /Users/artursjermolickis/projects/osmiocenter/venv/bin/python3
Python Version: 3.7.4
Python Path:    
['/Users/artursjermolickis/projects/osmiocenter/mysite',
 '/Users/artursjermolickis/projects/osmiocenter/venv/lib/python37.zip',
 '/Users/artursjermolickis/projects/osmiocenter/venv/lib/python3.7',
 '/Users/artursjermolickis/projects/osmiocenter/venv/lib/python3.7/lib-dynload',
 '/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7',
 '/Users/artursjermolickis/projects/osmiocenter/venv/lib/python3.7/site-packages',
 '/Users/artursjermolickis/projects/osmiocenter/venv/lib/python3.7/site-packages/odf',
 '/Users/artursjermolickis/projects/osmiocenter/venv/lib/python3.7/site-packages/odf',
 '/Users/artursjermolickis/projects/osmiocenter/venv/lib/python3.7/site-packages/odf',
 '/Users/artursjermolickis/projects/osmiocenter/venv/lib/python3.7/site-packages/odf',
 '/Users/artursjermolickis/projects/osmiocenter/venv/lib/python3.7/site-packages/odf',
 '/Users/artursjermolickis/projects/osmiocenter/venv/lib/python3.7/site-packages/odf',
 '/Users/artursjermolickis/projects/osmiocenter/venv/lib/python3.7/site-packages/odf']

如果您需要我提供更多代码,请告诉我您需要什么。

真的希望您能在这里帮助我。

这里是我的问题的其他评论。

需要通过数学函数过滤结果。

作为示例,我发布了safety_stock,提供的解决方案确实可以解决该问题。

但是正如您已经提到的,我需要通过一个稍微复杂的功能来对数据库进行排序,因此最好在models.py中进行操作,而不是在vews.py中进行处理,以便以后使用。所以我要替换为safety_stock的代码是:

def safe_stock(self):
        if self.safety_stock ==0:
            ss= (self.sales6mavgs()+self.stdev())*(self.lead_time+self.bookingterms)
        else:
            ss=Decimal(self.safety_stock)
        return Decimal(ss.quantize(Decimal('1'),rounding=ROUND_UP))

因此,从您的建议中,我确实了解到,我必须实现ExpressionWrapper

如何使用ExpressionWrapper实现

我知道,这个问题已经被提及过几次了,但是我找不到解决问题的方法。因此,请接受我的道歉以再次请求。我有一个基于条件的过滤器,没有...

python django filter
1个回答
2
投票

我不认为此dontshow=above_below_ss()是可行的,因为它是一个实例方法,这意味着您首先获取一条记录,然后将其命名为record.above_below_ss()。您可以改为使用funcexpression wrapper将这种方法编写为注释的一部分:

© www.soinside.com 2019 - 2024. All rights reserved.