Hello everyone,
I am building a simple chat web app with Django Channels and daphne.
In my code I want to store sent messages in the database. For that I created a “Message” Model in my app. When I try to import that model into consumers.py I get the error:
RuntimeError: Model class myproject.apps.chat.models.Message doesn’t declare an explicit app_label and isn’t in an application in INSTALLED_APPS.
The strange thing is, that I can import the model just fine into views.py.
Here is my models.py
from django.db import models
from django.utils.timezone import now
class Message(models.Model):
username = models.CharField('Name', max_length = 20)
text = models.TextField('Text')
datetime = models.DateTimeField("DateTime", default=now)
def __str__(self):
return self.username + ' ' + self.text
Here is the beginning of my consumers.py, the error comes already from line 2
from channels.generic.websocket import WebsocketConsumer
from .models import Message
import json
class chatConsumer(WebsocketConsumer):
def connect(self):
self.accept()
Here is the beginning of my views.py
from django.shortcuts import render, redirect
from django.utils.crypto import get_random_string
from .models import Message
def index(request):
Here are my INSTALLED_APPS in settings.py
INSTALLED_APPS = [
'daphne',
'channels',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'homepage.apps.HomepageConfig',
'chat.apps.ChatConfig',
]
I did change the folder structure and I will try do describe it here as best as I can:
myproject/
media/
myproject/
apps/
chat/
__init__.py
admin.py
apps.py
consumers.py
models.py
routing.py
tests.py
urls.py
views.py
asgi.py
settings.py
urls.py
wsgi.py
static/
templates/
db.sqlite3
manage.py
Maybe someone can help and I am fine to give more information if needed.
Thanks in advance