The problem of importing a model into consumer django Websockets
I want to write a consumer for the application, however, after importing the Device model, the application does not even want to start, crashes with an error that it cannot find the application settings.
# consumers.py
import json
from channels.generic.websocket import AsyncWebsocketConsumer
# from devices.models import Device
class DeviceConsumer(AsyncWebsocketConsumer):
async def connect(self):
self.device_id = self.scope['url_route']['kwargs']['device_id']
print(self.device_id)
await self.accept()
async def disconnect(self, close_code):
await self.close()
async def receive(self, text_data):
text_data_json = json.loads(text_data)
message = text_data_json['message']
print(message)
await self.send(text_data=json.dumps({'message': message}))
And here are the Device and User models:
from django.db import models
from users.models import User
from core.base.utils import generate_uuid
class Device(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
name = models.CharField(max_length=255)
device_key = models.CharField(max_length=255, unique=True, default=generate_uuid)
is_active = models.BooleanField(default=True)
def __str__(self):
return self.name
from django.db import models
from django.contrib.auth.models import AbstractUser
from django.utils.timezone import now
from django.utils.translation import gettext_lazy as _
class User(AbstractUser):
first_name = models.CharField(_("first_name "), max_length=60, blank=False)
last_name = models.CharField(_("last_name "), max_length=60, blank=False)
username = models.CharField(_("username"), max_length=60, unique=True)
email = models.EmailField(_("email address"), max_length=255, blank=False, unique=True)
is_staff = models.BooleanField(
_("is staff"),
default=False,
help_text=_("Determines whether the user can log in to this administrative site."),
)
is_active = models.BooleanField(
_("is active"),
default=True,
help_text=_(
"Indicates whether this user should be considered active."
"Uncheck this box instead of deleting accounts."
),
)
date_joined = models.DateTimeField(_("дата создания"), default=now)
USERNAME_FIELD = "username"
EMAIL_FIELD = "email"
REQUIRED_FIELDS = ["first_name", "last_name", "email"]
def __str__(self):
return f"{self.first_name} {self.last_name} ({self.username}) {self.email}"
class Meta:
verbose_name = _("user")
verbose_name_plural = _("users")
db_table = "users"
What could be the problem? I need to use the Device in consumer I tried everything that the Internet offered for this error...
The error that appears at the time of adding the import:
PS D:\MyProjects\V4Control\WakeHub\backend> uvicorn core.asgi:application --host 127.0.0.1 --port 8000
Traceback (most recent call last):
File "<frozen runpy>", line 198, in _run_module_as_main
File "<frozen runpy>", line 88, in _run_code
File "C:\Users\User\AppData\Local\Programs\Python\Python311\Scripts\uvicorn.exe\__main__.py", line 7, in <module>
File "C:\Users\User\AppData\Local\Programs\Python\Python311\Lib\site-packages\click\core.py", line 1157, in __call__
return self.main(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\User\AppData\Local\Programs\Python\Python311\Lib\site-packages\click\core.py", line 1078, in main
rv = self.invoke(ctx)
^^^^^^^^^^^^^^^^
File "C:\Users\User\AppData\Local\Programs\Python\Python311\Lib\site-packages\click\core.py", line 1434, in invoke
return ctx.invoke(self.callback, **ctx.params)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\User\AppData\Local\Programs\Python\Python311\Lib\site-packages\click\core.py", line 783, in invoke
return __callback(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\User\AppData\Local\Programs\Python\Python311\Lib\site-packages\uvicorn\main.py", line 409, in main
run(
File "C:\Users\User\AppData\Local\Programs\Python\Python311\Lib\site-packages\uvicorn\main.py", line 575, in run
server.run()
File "C:\Users\User\AppData\Local\Programs\Python\Python311\Lib\site-packages\uvicorn\server.py", line 65, in run
return asyncio.run(self.serve(sockets=sockets))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\User\AppData\Local\Programs\Python\Python311\Lib\asyncio\runners.py", line 190, in run
return runner.run(main)
^^^^^^^^^^^^^^^^
File "C:\Users\User\AppData\Local\Programs\Python\Python311\Lib\asyncio\runners.py", line 118, in run
return self._loop.run_until_complete(task)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\User\AppData\Local\Programs\Python\Python311\Lib\asyncio\base_events.py", line 653, in run_until_complete
return future.result()
^^^^^^^^^^^^^^^
File "C:\Users\User\AppData\Local\Programs\Python\Python311\Lib\site-packages\uvicorn\server.py", line 69, in serve
await self._serve(sockets)
File "C:\Users\User\AppData\Local\Programs\Python\Python311\Lib\site-packages\uvicorn\server.py", line 76, in _serve
config.load()
File "C:\Users\User\AppData\Local\Programs\Python\Python311\Lib\site-packages\uvicorn\config.py", line 433, in load
self.loaded_app = import_from_string(self.app)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\User\AppData\Local\Programs\Python\Python311\Lib\site-packages\uvicorn\importer.py", line 19, in import_from_string
module = importlib.import_module(module_str)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\User\AppData\Local\Programs\Python\Python311\Lib\importlib\__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "<frozen importlib._bootstrap>", line 1204, in _gcd_import
File "<frozen importlib._bootstrap>", line 1176, in _find_and_load
File "<frozen importlib._bootstrap>", line 1147, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 690, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 940, in exec_module
File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
File "D:\MyProjects\V4Control\WakeHub\backend\core\asgi.py", line 7, in <module>
from .routing import websocket_urlpatterns
File "D:\MyProjects\V4Control\WakeHub\backend\core\routing.py", line 3, in <module>
from devices import consumers
File "D:\MyProjects\V4Control\WakeHub\backend\devices\consumers.py", line 4, in <module>
from devices.models import Device
File "D:\MyProjects\V4Control\WakeHub\backend\devices\models.py", line 2, in <module>
from users.models import User
File "D:\MyProjects\V4Control\WakeHub\backend\users\models.py", line 2, in <module>
from django.contrib.auth.models import AbstractUser
File "C:\Users\User\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\contrib\auth\models.py", line 5, in <module>
from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
File "C:\Users\User\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\contrib\auth\base_user.py", line 40, in <module>
class AbstractBaseUser(models.Model):
File "C:\Users\User\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\db\models\base.py", line 129, in __new__
app_config = apps.get_containing_app_config(module)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\User\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\apps\registry.py", line 260, in get_containing_app_config
self.check_apps_ready()
File "C:\Users\User\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\apps\registry.py", line 137, in check_apps_ready
settings.INSTALLED_APPS
File "C:\Users\User\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\conf\__init__.py", line 81, in __getattr__
self._setup(name)
File "C:\Users\User\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\conf\__init__.py", line 61, in _setup
raise ImproperlyConfigured(
django.core.exceptions.ImproperlyConfigured: Requested setting INSTALLED_APPS, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
PS D:\MyProjects\V4Control\WakeHub\backend>
Although all the settings are present in place....