[PYTHON] Browse an existing external database with Django

Overview

Setting method

Added DB information

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',
    }
}

Set model

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)

View settings

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)

Don't forget to migrate

$ python3 ./manage.py makemigrations
$ python3 ./manage.py migrate

Recommended Posts

Browse an existing external database with Django
It's too easy to use an existing database with Django
Create an API with Django
Deploy an existing app with docker + pyenv-virtualenv + uwsgi + django
Rename an existing Django application
Load Django modules with an interpreter
Note: Send an email with Django
Create an update screen with Django Updateview
Internationalization with django
Azure External Storage MySQL Challenges with Django (PTVS)
CRUD with Django
Procedure for creating an application with Django with Pycharm ~ Preparation ~
Authenticate Google with Django
Django 1.11 started with Python3.6
Upload files with Django
Development digest with Django
Output PDF with Django
Markdown output with Django
Use Gentelella with django
Twitter OAuth with Django
Fix database with pytest-docker
Getting Started with Django 1
Send email with Django
File upload with django
Database search with db.py
Use LESS with Django
Pooling mechanize with Django
Use MySQL with Django
Start today with Django
Getting Started with Django 2
I'm trying to create an authentication / authorization process with Django
Create an authentication feature with django-allauth and CustomUser in Django
Create a Todo app with Django ① Build an environment with Docker