So I am trying to add a temporary attribute to two separate QuerySets from the same model, but it seems that as soon as I try to merge them together, the new fields disappear.
Example:
class IndexView(View):
def get(self, request):
q1 = MyModel.objects.filter(points=0)
q2 = MyModel.objects.filter(points=100)
for q in q1:
q.title = 'Loser'
print(q.title) # Prints 'Loser'
for q in q2:
q.title = 'Winner'
print(q.title) # Prints 'Winner'
q = q1 | q2 # Merges the two QuerySets together
for item in q:
print(item.title) # ERROR: "Title" is not a field anymore apparently...
return render(request, 'index.html', {})
I have tried another approach that uses .chain()
(mentioned in another thread), but that changes the type from QuerySet
to List
, which I do not want. I will post it below. Is there any way to keep the list as a QuerySet while achieving the same results? Please note that I want the title
attribute to be temporary so I can separate items in the template. Also, the points
filter is not the actual filter and is actually a lot more complicated, so template tags and model methods are not an option.
from itertools import chain
feed = sorted(
chain(q1, q2),
key=lambda instance: instance.created)
Django v1.9.6
Python v3.4.3