在python中处理列表

在python中处理列表,python,django,django-models,Python,Django,Django Models,我试图对django lfs项目进行一个小的修改,这将允许我停用没有库存的产品。不幸的是,我刚刚开始学习python,所以在语法方面遇到了很大的麻烦。这就是我想做的。如果我的产品是子类型,我将使用“is_variant”方法返回tru。如果它是一个变体,我转向母产品,获取它的活动变体并检查它们的库存。如果库存大于0,则活动变量为0,否则为1。如果通过变量循环后“活动”仍然为1,我将父产品的活动设置为false 不知怎么的,我无法使它正常工作。使用时: def deactivate(self

我试图对django lfs项目进行一个小的修改,这将允许我停用没有库存的产品。不幸的是,我刚刚开始学习python,所以在语法方面遇到了很大的麻烦。这就是我想做的。如果我的产品是子类型,我将使用“is_variant”方法返回tru。如果它是一个变体,我转向母产品,获取它的活动变体并检查它们的库存。如果库存大于0,则活动变量为0,否则为1。如果通过变量循环后“活动”仍然为1,我将父产品的活动设置为false

不知怎么的,我无法使它正常工作。使用时:

   def deactivate(self):
        if self.is_variant():
            prod = self.parent
            prod.active = all(var.get_stock_amount() != 0 for var in prod.variants.filter(active=True))
        else:
            prod.active = self.get_stock_amount() != 0

        self.parent.save()
        inactive = 0
        if self.is_variant():
            prod = self.parent
            for s in prod.variants.filter(active=True):
                if s.get_stock_amount() == 0:
                    inactive = 1
                else:
                    inactive = 0
            if inactive == 1:
                prod.active = 0
            prod.save()
        else:
            if self.get_stock_amount() == 0:
                self.active = 0

            self.save()
它使我的产品失效,无论它的变体是否有库存。使用时:

   def deactivate(self):
        if self.is_variant():
            prod = self.parent
            prod.active = all(var.get_stock_amount() != 0 for var in prod.variants.filter(active=True))
        else:
            prod.active = self.get_stock_amount() != 0

        self.parent.save()
        inactive = 0
        if self.is_variant():
            prod = self.parent
            for s in prod.variants.filter(active=True):
                if s.get_stock_amount() == 0:
                    inactive = 1
                else:
                    inactive = 0
            if inactive == 1:
                prod.active = 0
            prod.save()
        else:
            if self.get_stock_amount() == 0:
                self.active = 0

            self.save()
同样的情况也会发生,因此每次都会停用我的产品


我已经检查了shell中的返回类型,self是一个变量,它是活动的。

首先,我不会调用list
set
,因为这是一个Python内置方法(请参阅)。在列表上使用
append
(语法不正确,出现的错误会明确地告诉您这一点;),您必须在以下操作之前初始化列表:

def deactivate(self):
"""If there are no stocks, deactivate the product. Used in last step of checkout.
"""
if self.has_variants():
    sets = []
    for s in self.variants.filter(active=True):
        sets.append(s)  
    for var in sets:
        ...
但是,如果唯一的目的是再次迭代,那么为什么要事先创建一个列表呢?您只需执行以下操作:

def deactivate(self):
"""If there are no stocks, deactivate the product. Used in last step of checkout.
"""
if self.has_variants():
    for s in self.variants.filter(active=True):   
        if s.get_stock_amount() == 0:
            inactive = True
        else:
            inactive = False
else:
    ...

阅读更多信息。

此代码在许多方面都是错误的

  • 创建名为
    set
    的列表(如上所述)
  • 在循环中多次设置变量而不读取
  • 不需要的返回值(如果只有一个出口点,这怎么有用?)
  • 我认为这同样有效:

    def deactivate(self):
        """If there are no stocks, deactivate the product. Used in last step of checkout.
        """
        if self.has_variants():
            inactive = any(var.get_stock_amount() == 0 for var in self.variants.filter(active=True))
        else:
            inactive = self.get_stock_amount() == 0
        self.active = not inactive
    
    或者可能:

    def deactivate(self):
        """If there are no stocks, deactivate the product. Used in last step of checkout.
        """
        if self.has_variants():
            self.active = all(var.get_stock_amount() != 0 for var in self.variants.filter(active=True))
        else:
            self.active = self.get_stock_amount() != 0
    

    正确的解决方案。我想还有很多地方可以优化它,但首先我需要学习如何:):


    多一点上下文可能会有所帮助。上面的代码片段是您添加到项目中的吗?
    set[]=s
    行在许多级别上看起来不正确。可以发布错误输出吗?是的,这应该是我自己的附加组件。has_variant方法返回bool,get_stock_amount返回int,self.variants返回list。您说您只是在学习Python。你通常使用什么语言?这可能有助于我们了解您希望set[]=s做什么@格雷格·鲍尔:这看起来像PHP。如果
    set
    是一个数组,那么
    set[]=el
    会在该数组中附加一个元素。谢谢,我以前使用过这种方法,但肯定是混合了其他方法,因为它也会给我带来错误。@owca:Python的文档很棒,花20分钟查看它可以节省您的时间run@owca:查看我的更新答案。。。我意识到(花了一些时间)创建列表是完全没有必要的。