Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我可以不使用object.filter python而使用filter吗_Python_Django_List - Fatal编程技术网

我可以不使用object.filter python而使用filter吗

我可以不使用object.filter python而使用filter吗,python,django,list,Python,Django,List,我总是在Question.objects.filter()中使用filter 但这一次,我想使用filter从类中获取值,例如: class Expense(): amount = '' currency = '' date = '' 这是我的方法: def get_expense_list(self, taskId): #get expense list self.expenseList = [Expense()] # get all act

我总是在
Question.objects.filter()中使用filter
但这一次,我想使用filter从类中获取值,例如:

class Expense():
    amount = ''
    currency = ''
    date = '' 
这是我的方法:

def get_expense_list(self, taskId):
    #get expense list
    self.expenseList = [Expense()]
    # get all activity from task 
    self.activiytList = Activity.objects.filter(task_id=taskId)

    for activity in self.activiytList:
        try:    
            self.expenseList.filter(currency=activity.expense_currency, self.expenseList)
            self.expenseList.amount = self.expenseList.expense.amount + activity.expense
        except:
            newExpense = Expense()
            newExpense.currency = activity.currency
            newExpense.amount = activity.amount
            newExpense.date = activity.date
            self.expenseList.append(newExpense)
我正在从活动列表创建费用列表。在
活动.model
中,有以下对象:

amount =  models.DecimalField(max_digits=16, decimal_places=2)
currency =  models.CharField(max_length=5)
date = models.DateField()
我想在费用清单中显示,如果他们的货币和日期相同,金额将增加;如果没有现有的日期和货币,金额将
append()

请帮助我。

列表中没有
filter()
方法,因此您正在编写的代码无效

因此,如果您想要的是:

我想在费用列表中显示,如果他们的货币和日期相同,金额将增加;如果没有现有的日期和货币,金额将追加()

然后你会想做一些类似的事情:

def get_expense_list(self, taskId):
    self.expenseList = []
    self.activityList = Activity.objects.filter(task_id=taskId)

    for activity in self.activityList:
        # all existing expenses for this currency and date
        comparisonFunc = lambda x: x.currency == activity.currency and x.date == activity.date
        expenses = filter(comparisonFunc, self.expenseList)

        # if there are no expenses, create a new
        if len(expenses) == 0:
            expense = Expense()
            expense.amount = activity.amount
            expense.date = activity.date
            expense.currency = activity.currency
            self.expenseList.append(expense)
        # otherwise, increase the amount for expenses that were matching
        else:
            for expense in expenses:
                expense.amount += activity.amount

您也可以通过聚合在数据库中完成所有这些操作。

您好,非常感谢您理解我想展示的内容。实际上,我尝试运行它,并收到错误消息“filter类型的对象没有len()。非常感谢你的帮助