django: Manager isn't accessible via username_and_password instances

Viewed 77

I am facing a error in Django. Here is my code. Also I am working with databases here.

Here is my code in models.py:

from django.db import models
from django.db.models.fields.related import ForeignKey

# Create your models here.
class username_and_password(models.Model):
    user_name = models.CharField(max_length=300)
    password = models.CharField(max_length=300)
    def __str__(self):
        return self.password

And then I went to the cmd and made migrations. then opened the shell. So far everything worked. In shell I did

In [2]: from main.models import database, username_and_password

In [3]: data = username_and_password(user_name="Databasetest",password="Runningtest")

In [4]: data.save()

Everything worked but then when I typed:

data.objects.all()

It showed me an error:

AttributeError                            Traceback (most recent call last)
<ipython-input-4-d33a4311e29a> in <module>
----> 1 data.objects.all()

~\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\models\manager.py in __get__(self, instance, cls)
    177     def __get__(self, instance, cls=None):
    178         if instance is not None:
--> 179             raise AttributeError("Manager isn't accessible via %s instances" % cls.__name__)
    180
    181         if cls._meta.abstract:

AttributeError: Manager isn't accessible via username_and_password instances

Please help me on this, I am new to the library Django, Any help is appreciated .

1 Answers

You can only access the manager through a reference to the class, not a class object, so you access this with:

username_and_password.objects.all()

Note: Models in Django are written in PascalCase, not snake_case, so you might want to rename the model from username_and_password to UsernameAndPassword.

Related