[Django]-Manager isn't accessible via Blog instances

8👍

Your problem is in this line:

print [e.name for e in b.objects.all()]  # won't work

b is a Blog instance, which will not access the objects Manager. You might try this instead (if you want all rows, which it appears you do since you are creating a list from multiple names):

print [e.name for e in Blog.objects.all()]

Note the use of Blog instead of b in Blog.objects.all(). The objects manager is not accessible via b but is accessible via the class Blog.

For further explanation (using an example much like yours), see the docs here.

3👍

b.objects.all() should be Blog.objects.all() because the manager (objects) must be accessed from the model class not instances.

2👍

Well, as the error says, the Manager (.objects) is only available from the Blog class, not from its instances.

But it’s not clear what you are trying to do. What are you expecting the list comprehension to return?

Leave a comment