[Fixed]-Django: query various objects in a loop

1👍

You can make the list contain the actual classes, then your code will work:

information_objects = [Cooperation, Article, City]

If you absolutely need to get them based on strings, you can get the classes from globals():

information_objects = [globals()[name] for name in ('Cooperation', 'Article', 'City')]

or use getattr if they are in a specific module:

information_objects = [getattr(the_module, name) for name in ('Cooperation', 'Article', 'City')]

Leave a comment