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 | 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)