writes_on
writes_on

Reputation: 1885

How can I add an attribute to a queryset in Django

I've got a Django 2.2.28 legacy application running under Python 3.7.7. I've got a queryset I'd like to add data to that's not from the database, but generated by Python. So something like this:

for item in queryset.iterator():
    item.new_property = python_generated_property_value()

I want this new_property to be available in a template.

Upvotes: 0

Views: 238

Answers (1)

oruchkin
oruchkin

Reputation: 1295

you can create a dictionary, and pass data in a template with it

new_dict = {}
for item in queryset.iterator():
    new_dict["item_property"] = python_generated_property_value()
    

this will give you a dictionary in a same order as it is in your queryset, and you could "for loop" this dict in parallel with your queryset in a template

Upvotes: 1

Related Questions