troubleshooting Question

Dynamic Python Syntax

Avatar of mannsi
mannsi asked on
Python
5 Comments1 Solution374 ViewsLast Modified:
Hello experts,

I am not sure if I it is possible to do what I want to do. Consider the following two lines.

document_type = 'Book'
documents = bookModel.document_type.objects.all()

document_type can be 'Book', 'Magazine', 'Essay' etc.
I don't want to have to write an if sentence for each type like this :
if document_type == 'Book':
    documents = bookModel.Book.objects.all()

Can I somehow make it work like the first two lines of code ?
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 5 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 5 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros