-1

"""Please, I have created some model classes in my django app.models. I want to access to objects of any of the models in my views based on request.

My view looks like this:

def profile(request, course):
    Course_name = course
    Contents = model.objects.get()
    context = {obj: contents}
    return render(request, "pro.html", context) 

"""My question is, I want to replace model in model.objects.get() with the variable "course_name" But am getting this error, 'str' object has no attribute 'objects' Please Help"""

1 Answers1

-1

Try using eval(model).objects instead of model.objects

The problem is that you are trying to read objects attribute from a string object which is not valid. you should first try to get the class which its name is equal to the value of model string and eval is the way to do that.

Warning: The class which you want to access using eval should be visible in your code scope. So, import all possible models that you may need to use, and also put your code in a try except block

I hope this answers your question