Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Manager isn't accessible via Blog instances

I am using Django(1.5.4), Python(2.7), sqlite3.

I want to save my user details in sqlite3 database. My code is like this;

This is models.py file.

from django.db import models

class Blog(models.Model):
    name = models.CharField(max_length=100)
    tagline = models.TextField()

    def __unicode__(self):
        return self.name

This is my views.py file

def shiva(request):
    b = Blog(name='Itons Blog', tagline='All the best to Iton team')
    b.save()
    print [e.name for e in b.objects.all()]
    return HttpResponse("saved")

When i am trying to save the details in sqlite3 the error is coming as;

AttributeError at / Manager isn't accessible via Blog instances

like image 776
Shiva Avatar asked Jan 26 '26 18:01

Shiva


1 Answers

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.

like image 166
Justin O Barber Avatar answered Jan 29 '26 08:01

Justin O Barber