Remove column from queryset element

I have a simple model:

class Table(models.Model):
    field_1= models.TextField(blank=True)
    field_2= models.TextField(blank=True)
    field_3= models.TextField(blank=True)

that I query:

table= Table.objects.all()

when I loop the QuerySet I would like to remove a column from the object and save it in a separate object, something like:

 for item in table: 
      item_less=item
      item_less.exlude(field_1)  # just an example, exclude doesn't work!  

I would like to end up with a list of records without field_1.
Of course I don’t want to delete field_1 I just don’t want it to show in the second list.

Many thanks

You can use exclude like this…

# table variable contain all the queryset data
table = Table.objects.all()
item_less = []

for item in table:
    # At here you can give some conditions as per your choice
    item_less.append(item)
    # when using exclude, it remove the object from queryset i.e table with certain condition like id=item.id
    table.exclude(id=item.id)

Read more about it from this part of docs QuerySet API reference | Django documentation | Django

not sure it would get me the desired result.

I want to remove specific columns from each element in the queryset.

Let’s say I have the following table

Field 1 Field 2 Field 3
abc def yxz
123 456 789

I would like to do something like this:

table = Table.objects.all()
for item in table:
 item_less = item
     if item.field_1 == 'abc':
         item_less.remove(Field_1)
     if item.field_1 == '123' 
         item_less.remove(Field_2)
     print(item)
     print(item_less)

output:
item(1): ‘abc’, ‘def’, ‘yxz’
item_less(1): ‘def’, ‘yxz’ # FIELD 1 removed from list

item(2): ‘123’, ‘456’, ‘789’
item_less(2): ‘123’, ‘789’ # FIELD 2 removed from list

Basically I need to remove specific fields from the table object (without deleting data of course)

In this case you can use only or defer.

Example:
table= Table.objects.only("field_1", "field_2")

or:
table= Table.obects.defer("field_3")

Actually, I would ask why you’re trying to do this.

If it’s an issue of what you’re rendering, just don’t render that field.

If it’s an issue that you’re serializing that data and don’t want it included, then that’s up to the serializer.

I want to find out which fields in each records are present in a separate entity, let me write a longer example:

class student 1 student 2 student 3 student 4
class A John robert jane sara
class B Mike silvia tony eric
class C jane livia robert jack

Now I have a list of students that took an exams
exam 1: John, tony, eric, livia
exam 2; eric, jane

for exam in exams:    # loop exams
  for class in classes:  # loop classes 
       remaning_class=class  # list of students that haven't taken the exam yet
       if student_1 in exam: # pseudo-code 
            remaining_class.remove(student_1)
       if student_2 in exam:
            remaining_class.remove(student_2)
....

output:
remaining_class(1): “Robert”, “sara”
remaining_class(2):“mike”, “silvia”, “tony”
remaining_class(3): “robert”, “jack”

the idea is to end up with a list of students that didn’t attend ANY exams
so my idea was to loop the exams and step by step removing from each class students that gave at least one exam. ending up with shorter class lists (with only the remaining students).

I hope it makes sense

I’m sorry, your example doesn’t appear to be internally consistent. Your table above references “class A”, “class B” and “class C”. I don’t see how that relates to “exam 1” and “exam 2”. Am I supposed to infer that exam 1 is the one and only exam for class A? Or does it not matter because you’re only looking for any student not appearing in any exam list?

But more fundamentally, I think you need to rework and normalize your data structure. (But without seeing the actual models involved, I can’t really offer any tangible suggestions.)

Anyway, for what you have, what I would suggest that you retrieve your data as a Python list of lists using the values_list function, and convert all the students into a flat list of names. Then filter out the names based upon the exam lists.

thanks, I can’t really modify the underlying data structure, not at this stage

just for clarification:
I have a table with classes populated with students
I have a table with exams populated with students that took an exam

there’s no relationship between the 2, (student data is preloaded, exam data is dynamically loaded),
ANY student can attend ANY exam
any student that doesn’t attend exams get a negative mark.
that’s why I need to loop all classes and all exams and find out who has given exams and who hasn’t
it doesn’t matter which exam, any student that hasn’t attended any exam gets a notification (that’s why I need a subset of each class with the list of students that didn’t attend any exams.

I’ll look at your value_list suggestion, thanks.