create a dictionary of keys as attributes and values as model objects

this might do this task

blog_dict = {}
blogs = Blog.objects.all()
for blog in blogs:
    blog_dict[blog.title] = blog

as @ZXYNINE mentioned in the comment section below, it could be done with a single line instead of full form of for loop like so:

blog_dict  = { blog.title:blog for blog in blogs}

I find the first method is better for beginners, but also it worth to mention the other method as well.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top