Django:参考中的上一项模型.保存()方法失败

2024-06-26 00:12:20 发布

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

我正在尝试为带有队列验证的项目实现一个等候室。
一般的想法是把项目放到等候室,计算它的计数器(值定义需要多少新项目来确认这个项目),在添加每个新项目后减少它,最后在将计数器减到零以下后确认它。如果等候室中已经有项目,则计算的计数器将增加上一个项目的计数器,以便将项目放入队列。
我的实现非常简单,但我发现这个解决方案相当快。 但引用前一项对我来说不起作用,我也找不到原因——它通常从等待对象返回“random”(或至少不是最后一个)值。在

以下是重要的代码片段:

class Waiting(models.Model):                                                       
    item = models.ForeignKey(Item)                                             
    counter = models.FloatField(default=0)                                         
    (...)
    def clearup(self): 
        (...) #here is decrementing and confirming part - it's working fine
    def save(self, update=False):
        if update:                                                                 
            return super(Waiting, self).save()                                     

        item = self.item                                                       
        self.clearup()
        (...) #nothing important
        self.counter = item.quantity * items_list[item.name][1]
        last = Waiting.objects.exclude(                                        
                item__name="Something I don't want here").order_by('-pk')
        if last:
            last = last[0]
            weight = items_list[last.item.name][1]
            self.counter += (last.item.quantity * weight)                       

        super(Waiting, self).save()

编辑:我是弱智。在


Tags: 项目nameselfhere队列modelssavedef