I want to run a command for the duration of the lifetime of the website. I don't want to run it more than once.
Let's say I want to run the query:
set names utf8mb4;
Then I would just run something like:
SomeRandomObject.objects.raw('set names utf8mb4;')
Where should I put this? Does it matter what object I run the query on? Is there a better object?
I usually do this off the
connection
object itself.This way you can avoid having to use some random model to run your raw queries.
n.b. if you only have one database connection, i.e.,
default
you can use:To run this once at startup, you can add the following to your
DATABASES