Ho il seguente:Come convertire un Django QuerySet a un elenco
answers = Answer.objects.filter(id__in=[answer.id for answer in answer_set.answers.all()])
poi:
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)
ottengo un errore:
'QuerySet' object has no attribute 'remove'
ho provato tutti i tipi per convertire QuerySet in un set o elenco standard. Niente funziona.
Come è possibile rimuovere un elemento dal QuerySet in modo che non lo elimini dal database e non restituisca un nuovo QuerySet (poiché si trova in un ciclo che non funzionerà)?
ho aggiunto un'altra riga al mio esempio di codice qui sopra, eliminando la stessa voce da exising_question_answers. è possibile usare un ifilter per quello anche in qualche modo? – john
Lo contrassegnerò come corretto perché non sapevo del filtro e mi ero dimenticato di lambda. – john