Aman Nepali
Aman Nepali

Reputation: 11

AttributeError at /BRMapp/view-books/

I am developing Book Record Management system as my first project on django but after performing all my functionalities I got a problem in my views.py file in a function name view-book in a line(books=models.Book.objects.all()).Although i have made a class name 'Book' in models.py and i also have imported models in my views.py file with a line(form django.db import models).Although it is not working.Kindly help Error showing like this:::

module 'django.db.models' has no attribute 'Book'
Request Method: GET
Request URL:    http://localhost:8000/BRMapp/view-books/
Django Version: 3.2.5
Exception Type: AttributeError
Exception Value:    
module 'django.db.models' has no attribute 'Book'
Exception Location: G:\Django projects\firstproject\BRMapp\views.py, line 41, in viewBooks
Python Executable:  C:\Users\amann\AppData\Local\Programs\Python\Python39\python.exe
Python Version: 3.9.2
Python Path:    
['G:\\Django projects\\firstproject',
 'C:\\Users\\amann\\AppData\\Local\\Programs\\Python\\Python39\\python39.zip',
 'C:\\Users\\amann\\AppData\\Local\\Programs\\Python\\Python39\\DLLs',
 'C:\\Users\\amann\\AppData\\Local\\Programs\\Python\\Python39\\lib',
 'C:\\Users\\amann\\AppData\\Local\\Programs\\Python\\Python39',
 'C:\\Users\\amann\\AppData\\Roaming\\Python\\Python39\\site-packages',
 'C:\\Users\\amann\\AppData\\Local\\Programs\\Python\\Python39\\lib\\site-packages']
Server time:    Thu, 12 Aug 2021 10:19:08 +0000

Upvotes: 1

Views: 38

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 477210

Based on the exception models points to the models submodule of the django.db module, but there is no Book defined in that module.

Likely the Book originates from elsewhere, from an appname.models module.

You thus import this with:

from appname.models import Book

# …

def viewBooks(request):
    # …
    #        ↓ no models.
    books = Book.objects.all()
    # …

Upvotes: 0

Related Questions