我有以下几点:如何Django的查询集转换到一个列表
answers = Answer.objects.filter(id__in=[answer.id for answer in answer_set.answers.all()])
再后来:
for i in range(len(answers)):
# iterate through all existing QuestionAnswer objects
for existing_question_answer in existing_question_answers:
# if an answer is already associated, remove it from the
# list of answers to save
if answers[i].id == existing_question_answer.answer.id:
answers.remove(answers[i]) # doesn't work
existing_question_answers.remove(existing_question_answer)
我得到一个错误:
'QuerySet' object has no attribute 'remove'
我试着各种将QuerySet转换为标准集或列表。什么都没有
如何从QuerySet中删除项目,以便它不会从数据库中删除它,并且不会返回新的QuerySet(因为它在循环中不起作用)?
我在上面的代码示例中添加了一行,从exising_question_answers删除了相同的条目。有没有可能为此使用ifilter? – john 2010-12-12 22:43:33
我会将此标记为正确的,因为我不知道过滤器并忘记了有关lambda表达式。 – john 2010-12-13 20:10:13