Home:ALL Converter>Django Channels and Accessing Models

Django Channels and Accessing Models

Ask Time:2022-02-04T02:16:42         Author:TheStranger

Json Formatter

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?

Author:TheStranger,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/70976313/django-channels-and-accessing-models