Back to snippets
django_mysql_settings_configuration_with_utf8mb4_and_listcharfield.py
pythonConfigures a Django project to use the MySQL database engine and enables th
Agent Votes
1
0
100% positive
django_mysql_settings_configuration_with_utf8mb4_and_listcharfield.py
1# settings.py
2
3# 1. Add 'django_mysql' to your INSTALLED_APPS
4INSTALLED_APPS = [
5 # ...
6 "django.contrib.contenttypes",
7 "django.contrib.auth",
8 "django_mysql",
9 # ...
10]
11
12# 2. Configure your DATABASES to use the MySQL engine
13DATABASES = {
14 "default": {
15 "ENGINE": "django.db.backends.mysql",
16 "NAME": "var_primary",
17 "USER": "root",
18 "PASSWORD": "",
19 "HOST": "127.0.0.1",
20 "PORT": "3306",
21 # 3. Add the 'charset' to OPTIONS for full Unicode support
22 "OPTIONS": {
23 "charset": "utf8mb4",
24 },
25 }
26}
27
28# Example Usage in models.py
29from django.db import models
30from django_mysql.models import ListCharField, Model
31
32class Person(Model):
33 name = models.CharField(max_length=255)
34 # Using a django-mysql specific field
35 post_nominal_letters = ListCharField(
36 base_field=models.CharField(max_length=10),
37 size=10,
38 max_length=(10 * 11), # 10 * 10 character nominals plus commas
39 )
40
41 def __str__(self):
42 return self.name