settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': db,
'USER': user,
'PASSWORD': password,
'HOST': 'mysql01',
'PORT': '3306',
},
'otherdb': {
'ENGINE': 'django.db.backends.mysql',
'NAME': otherdb,
'USER': user,
'PASSWORD': password,
'HOST': 'mysql02'
'PORT': '3306',
}
}
application/models.py
from django.db import models
class <name of the class>(models.Model):
class Meta:
db_table = '<table name>'
id = models.IntegerField(primary_key=True)
name = models.CharField(max_length = 255)
application/views.py
from django.shortcuts import render
from .models import <name of the class>
def index(request):
data = <name of the class>.objects.db_manager("<DB name>").all()
params = { 'data': data }
return render(request, 'sample.html', params)
$ python3 ./manage.py makemigrations
$ python3 ./manage.py migrate
Recommended Posts