GithubHelp home page GithubHelp logo

multiple-db's Introduction

concept penggunaan multiple database

Config Multiple database

DATABASES = {
    'default': {
        'ENGINE'    : config("DB1_CONNECTION", default='django.db.backends.postgresql_psycopg2'),
        'USER'      : config('DB1_USER', default='root'),
        'NAME'      : config('DB1_NAME'),
        'PASSWORD'  : config('DB1_PASSWORD', default=''),
        'HOST'      : config('DB1_HOST', default='localhost'),
        'PORT'      : config('DB1_PORT', default='5432'),
    },
    'db_us': {
        'ENGINE'    : config("DB2_CONNECTION", default='django.db.backends.postgresql_psycopg2'),
        'USER'      : config('DB2_USER', default='root'),
        'NAME'      : config('DB2_NAME'),
        'PASSWORD'  : config('DB2_PASSWORD', default=''),
        'HOST'      : config('DB2_HOST', default='localhost'),
        'PORT'      : config('DB2_PORT', default='5432'),
    },

}

create data ke databa sesuai lokasi untuk menentukan database yang digunakan

 def create(self, request, *args, **kwargs):
    # jika country == US maka database untuk    menyimapan menggunakan db_us
    if request.data['country'] == 'US':
        serializer = self.get_serializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        serializer.save(using='db_us')
        return response.Response(serializer.data)
    return super().create(request, *args, **kwargs)

get all semua data dari 2 databae

# code

def list(self, request, *args, **kwargs):
    ``` list gabuangan dari dua database  ```
    data = list(chain(Warga.objects.using('db_us'), self.queryset))
    serialize = self.serializer_class(data, many=True)
    return response.Response(serialize.data)

[
	{
		"id": 1,
		"country": "US",
		"name": "RObin",
		"gender": "Fimale",
		"age": 2,
		"address": "Jl. Bantul km. 9, Juron, Rt/Rw : 19/00, Krandohan, Pendowoharjo, Kec. Sewon, Bantul, Daerah Istimewa Yogyakarta 55186"
	},
	{
		"id": 2,
		"country": "US",
		"name": "Vincent",
		"gender": "Male",
		"age": 12,
		"address": "LA"
	},
	{
		"id": 21,
		"country": "US",
		"name": "MARSELO",
		"gender": "Male",
		"age": 23,
		"address": "LA city"
	},
	{
		"id": 23,
		"country": "US",
		"name": "ANTONIA",
		"gender": "Fimale",
		"age": 23,
		"address": "LA city"
	},
	{
		"id": 1,
		"country": "IN",
		"name": "M Hadi Sasmito",
		"gender": "Fimale",
		"age": 12,
		"address": "Jl. Bantul km. 9, Juron, Rt/Rw : 19/00, Krandohan, Pendowoharjo, Kec. Sewon, Bantul, Daerah Istimewa Yogyakarta 55186"
	},
	{
		"id": 2,
		"country": "IN",
		"name": "M Hadi Sasmito",
		"gender": "Male",
		"age": 12,
		"address": "Jl. Bantul km. 9, Juron, Rt/Rw : 19/00, Krandohan, Pendowoharjo, Kec. Sewon, Bantul, Daerah Istimewa Yogyakarta 55186"
	},
	{
		"id": 17,
		"country": "IN",
		"name": "Tomy",
		"gender": "Male",
		"age": 23,
		"address": "LA city"
	},
	{
		"id": 22,
		"country": "IN",
		"name": "Anton",
		"gender": "Fimale",
		"age": 23,
		"address": "LA city"
	},
	{
		"id": 25,
		"country": "IN",
		"name": "ANTONIA",
		"gender": "Fimale",
		"age": 23,
		"address": "LA city"
	}
]

Data untuk db_us

+----+---------+---------+--------+-----+-----------------------------------------------------------------------------------------------------------------------+
| id | country | name    | gender | age | address                                                                                                               |
+----+---------+---------+--------+-----+-----------------------------------------------------------------------------------------------------------------------+
|  1 | US      | RObin   | Fimale |   2 | Jl. Bantul km. 9, Juron, Rt/Rw : 19/00, Krandohan, Pendowoharjo, Kec. Sewon, Bantul, Daerah Istimewa Yogyakarta 55186 |
|  2 | US      | Vincent | Male   |  12 | LA                                                                                                                    |
| 21 | US      | MARSELO | Male   |  23 | LA city                                                                                                               |
| 23 | US      | ANTONIA | Fimale |  23 | LA city                                                                                                               |
+----+---------+---------+--------+-----+-----------------------------------------------------------------------------------------------------------------------+

Data untuk db_id

+----+---------+----------------+--------+-----+-----------------------------------------------------------------------------------------------------------------------+
| id | country | name           | gender | age | address                                                                                                               |
+----+---------+----------------+--------+-----+-----------------------------------------------------------------------------------------------------------------------+
|  1 | IN      | M Hadi Sasmito | Fimale |  12 | Jl. Bantul km. 9, Juron, Rt/Rw : 19/00, Krandohan, Pendowoharjo, Kec. Sewon, Bantul, Daerah Istimewa Yogyakarta 55186 |
|  2 | IN      | M Hadi Sasmito | Male   |  12 | Jl. Bantul km. 9, Juron, Rt/Rw : 19/00, Krandohan, Pendowoharjo, Kec. Sewon, Bantul, Daerah Istimewa Yogyakarta 55186 |
| 17 | IN      | Tomy           | Male   |  23 | LA city                                                                                                               |
| 22 | IN      | Anton          | Fimale |  23 | LA city                                                                                                               |
| 25 | IN      | ANTONIA        | Fimale |  23 | LA city                                                                                                               |
+----+---------+----------------+--------+-----+-----------------------------------------------------------------------------------------------------------------------+
5 rows in set (0.01 sec)

multiple-db's People

Contributors

keselyoleren avatar

Watchers

James Cloos avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.