Skip to content

Commit

Permalink
add Django multi-db
Browse files Browse the repository at this point in the history
  • Loading branch information
twtrubiks committed May 31, 2017
1 parent ba018e4 commit c42ec6d
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 9 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,5 @@ ENV/

# Rope project settings
.ropeproject
.idea/
.idea/
settings_db_string.py
48 changes: 47 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@ Disabling Collectstatic

詳細教學可參考 [如何在 heroku 上使用 database](https://github.com/twtrubiks/Deploying-Flask-To-Heroku#%E5%A6%82%E4%BD%95%E5%9C%A8-heroku-%E4%B8%8A%E4%BD%BF%E7%94%A8-database)


```python
DATABASES = {
'default': {
Expand All @@ -125,6 +124,53 @@ DATABASES = {

![](http://i.imgur.com/KsQyZ2f.png)

## Django multiple-databases

順便介紹在 [Django](https://www.djangoproject.com/) 中的 multiple-databases,

使用方法, 將 [settings.py](https://github.com/twtrubiks/Deploying_Django_To_Heroku_Tutorial/blob/master/ptt_beauty_images/settings.py) 裡的 DATABASES 修改成如下

```python
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': os.environ.get('DATABASE_NAME'),
'USER': os.environ.get('DATABASE_USER'),
'PASSWORD': os.environ.get('DATABASE_PASSWORD'),
'HOST': os.environ.get('DATABASE_HOST'),
'PORT': os.environ.get('DATABASE_PORT'),
},
'': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': os.environ.get('DATABASE_NAME_2'),
'USER': os.environ.get('DATABASE_USER_2'),
'PASSWORD': os.environ.get('DATABASE_PASSWORD_2'),
'HOST': os.environ.get('DATABASE_HOST_2'),
'PORT': os.environ.get('DATABASE_PORT_2'),
}
}
```

這樣就代表你有兩個 DATABASES,更多資料可參考 [Django multi-db](https://docs.djangoproject.com/en/1.11/topics/db/multi-db/#defining-your-databases)

如果要指定 DATABASES 也非常容易,

假設我今天要用 'default' 這個 DB ,可以寫這樣

```python
Image.objects.using('default').all()
```

假設我今天要用 'db2' 這個 DB,可以寫這樣

```python
Image.objects.using('db2').all()
```

有沒有發現,其實就是 using('database name') 這樣,非常簡單

更多資料可參考 [Django manually-selecting-a-database](https://docs.djangoproject.com/en/1.11/topics/db/multi-db/#manually-selecting-a-database)

## 特色

* 使用 [lazyload](https://github.com/verlok/lazyload) 載入大量圖片。
Expand Down
22 changes: 21 additions & 1 deletion images/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,34 @@

from images.models import Image
from images.serializers import ImageSerializer
from ptt_beauty_images import settings


def index(request):
# single-databases
def index_old(request):
return render(request, 'index.html', {
'images': Image.objects.values('id', 'Url').order_by('-CreateDate')
})


# multiple-databases
def index(request):
images_seq = []
for db_name in settings.DATABASES:
query = Image.objects.using(db_name).all()
for data in query:
dict_image = {
'id': data.id,
'Url': data.Url,
'CreateDate': data.CreateDate
}
images_seq.append(dict_image)
images_seq = sorted(images_seq, key=lambda x: x['CreateDate'], reverse=True)
return render(request, 'index.html', {
'images': images_seq
})


# Create your views here.
class ImageViewSet(viewsets.ModelViewSet):
"""
Expand Down
14 changes: 8 additions & 6 deletions ptt_beauty_images/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.10/howto/deployment/checklist/

Expand All @@ -27,7 +26,6 @@

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = [
Expand Down Expand Up @@ -71,7 +69,6 @@

WSGI_APPLICATION = 'ptt_beauty_images.wsgi.application'


# Database
# https://docs.djangoproject.com/en/1.10/ref/settings/#databases

Expand All @@ -83,10 +80,17 @@
'PASSWORD': os.environ.get('DATABASE_PASSWORD'),
'HOST': os.environ.get('DATABASE_HOST'),
'PORT': os.environ.get('DATABASE_PORT'),
},
'db2': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': os.environ.get('DATABASE_NAME_2'),
'USER': os.environ.get('DATABASE_USER_2'),
'PASSWORD': os.environ.get('DATABASE_PASSWORD_2'),
'HOST': os.environ.get('DATABASE_HOST_2'),
'PORT': os.environ.get('DATABASE_PORT_2'),
}
}


# Password validation
# https://docs.djangoproject.com/en/1.10/ref/settings/#auth-password-validators

Expand All @@ -105,7 +109,6 @@
},
]


# Internationalization
# https://docs.djangoproject.com/en/1.10/topics/i18n/

Expand All @@ -119,7 +122,6 @@

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.10/howto/static-files/
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
Expand Down

0 comments on commit c42ec6d

Please sign in to comment.