djangopostgresqldjango-channelsdaphne

Django Channels and Accessing Models


I am running Django 3.2.10 and I am using Django Channels with daphne and gunicorn. I can get my websocket code to run and function without issue but when I go to access any of my django models I hit a snag. I'm doing something like this:

async def receive(self):
   modelselected = await database_sync_to_async(self.get_model)()

def get_model(self):
   return MyModel.objects.all()[0]

When I do this which is what the official docs suggest I do, I get the following error from daphne:

daphne | ERROR Exception inside application: could not connect to server: No such file or directory Is the server running locally and accepting connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?

My Django application works perfectly fine and I connect to the database no problem. I am at a loss to what I'm doing wrong. And my Postgres is running on Port 5432. Any thoughts?


Solution

  • So figured out what was wrong. I am using docker-compose and on creating the containers, I never shared the DB variables in the environment of the daphne server. As I was focused on the websockets it never arose until I tried to access the db via the Django model. Anyways all is good now but I'll leave this here in case someone was as silly as I was!