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 <nom de la classe>(models.Model):
class Meta:
db_table = '<nom de la table>'
id = models.IntegerField(primary_key=True)
name = models.CharField(max_length = 255)
application/views.py
from django.shortcuts import render
from .models import <nom de la classe>
def index(request):
data = <nom de la classe>.objects.db_manager("<Nom de la base de données>").all()
params = { 'data': data }
return render(request, 'sample.html', params)
$ python3 ./manage.py makemigrations
$ python3 ./manage.py migrate
Recommended Posts