Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3493270
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T11:51:43+00:00 2026-05-18T11:51:43+00:00

I have the following: answers = Answer.objects.filter(id__in=[answer.id for answer in answer_set.answers.all()]) then later: for

  • 0

I have the following:

answers = Answer.objects.filter(id__in=[answer.id for answer in answer_set.answers.all()])

then later:

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)

I get an error:

'QuerySet' object has no attribute 'remove'

I’ve tried all sorts to convert the QuerySet to a standard set or list. Nothing works.

How can I remove an item from the QuerySet so it doesn’t delete it from the database, and doesn’t return a new QuerySet (since it’s in a loop that won’t work)?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T11:51:44+00:00Added an answer on May 18, 2026 at 11:51 am

    You could do this:

    import itertools
    
    ids = set(existing_answer.answer.id for existing_answer in existing_question_answers)
    answers = itertools.ifilter(lambda x: x.id not in ids, answers)
    

    Read when QuerySets are evaluated and note that it is not good to load the whole result into memory (e.g. via list()).

    Reference: itertools.ifilter

    Update with regard to the comment:

    There are various ways to do this. One (which is probably not the best one in terms of memory and time) is to do exactly the same :

    answer_ids = set(answer.id for answer in answers)
    existing_question_answers = filter(lambda x: x.answer.id not in answers_id, existing_question_answers)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been following the answer of this question: How to update existing object
Reading a book I have found the following statement: (Object) Behaviors answer either of
FINAL EDIT: After following the answer from Darin Dimitrov, I have found that the
Can someone recommend a hosted solution that answers the following requirements (I have seen
Following this question and answer , I still have a bit trouble in the
I have seen many articles and Questions/Answers Regarding Lock Escalation but following things are
I have the following data structure: Object _id: ca06ef84-5934-498e-9887-8a3241e713f2 answers: Object owner: 273b7291-df2b-494c-bd9b-64e71283447e Topic:
I have lots of functions of the following template: function (field, filter){ var q
I've tried the answer suggested here , which follows with nearly all answers to
I tried the following to detach a graph of entity objects, and then attach

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.