При попытке запросить или добавить какие-либо данные в модалы, я получаю сообщение об ошибке type object 'Rooms' has no attribute 'objects'
. Вот что у меня есть:
Home / models.py:
class Rooms(models.Model):
room_no = models.AutoField(primary_key=True)
room_name = models.TextField(max_length=40)
Home / url.py:
from django.urls import path
from .views import HomePage, EnergyGeneration, RoomPage
urlpatterns = [
path('', HomePage.as_view(), name='homepage'),
path('energy', EnergyGeneration.as_view(), name='energygeneration'),
path('room/<int:room_no>/', RoomPage.as_view(), name='roompage'),
]
MainProjectApp / urls.py:
from django.contrib import admin
from django.urls import path, include
from .forms import UpdatedLoginView
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('Home.url')),
path('accounts/login/', UpdatedLoginView.as_view()),
path('accounts/', include('django.contrib.auth.urls')),
]
Home / views.py:
from Home.models import Rooms, Plugs, PlugElectricityConsumption, EnergyGeneration, EnergyMode, Battery, PowerTransaction, PowerGeneration
class HomePage(TemplateView):
template_name = 'home/index.html'
def get(self, request, *args, **kwargs):
rooms = Rooms.objects.all()
......
......
return render(request, self.template_name)
Вот полная трассировка стека ошибки:
Traceback (most recent call last):
File "E:\Uni\Year 3\Professional Development\Esteem_v2\venv\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
response = get_response(request)
File "E:\Uni\Year 3\Professional Development\Esteem_v2\venv\lib\site-packages\django\core\handlers\base.py", line 115, in _get_response
response = self.process_exception_by_middleware(e, request)
File "E:\Uni\Year 3\Professional Development\Esteem_v2\venv\lib\site-packages\django\core\handlers\base.py", line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "E:\Uni\Year 3\Professional Development\Esteem_v2\venv\lib\site-packages\django\views\generic\base.py", line 71, in view
return self.dispatch(request, *args, **kwargs)
File "E:\Uni\Year 3\Professional Development\Esteem_v2\venv\lib\site-packages\django\views\generic\base.py", line 97, in dispatch
return handler(request, *args, **kwargs)
File "E:\Uni\Year 3\Professional Development\Esteem_v2\Home\views.py", line 62, in get
rooms = Rooms.objects.all()
Exception Type: AttributeError at /
Exception Value: type object 'Rooms' has no attribute 'objects'
Если я попытаюсь запросить эти модалы с консоли Python , это работает:
from Home.models import Rooms
Rooms.objects.all()
<QuerySet [<Rooms: Rooms object (1)>]>
Но в Django это не так. Я не могу понять, что происходит.
Edit
Вот результат печати (тип (Rooms)) и print (dir (Rooms)):
<class 'type'>
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__mod
ule__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_allowed_methods', 'as_view', 'content_type', 'dis
patch', 'extra_context', 'get', 'get_context_data', 'get_template_names', 'http_method_names', 'http_method_not_allowed', 'options', 'render_to_response', 'response_class', 'setup', 'template_engine
', 'template_name']