I had created a new database and configured my files as in the Django documentation.
now I can access to the database via the Admin page and add some tables and data.
this is my settings.py configurations.
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
},
'measure_db': {
'NAME': 'measure_name',
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'measure.db.sqlite3'),
}
}
DATABASE_ROUTERS =['routers.db_routers.AuthRouter','routers.db_routers.Measure']
and I created a folder (routers) in it another file db_routers.py that contains this code:
from os import read
class AuthRouter:
route_app_labels = {'auth', 'contenttypes','sessions','admin'}
def db_for_read(self, model, **hints):
if model._meta.app_label in self.route_app_labels:
return 'default'
return None
def db_for_write(self, model, **hints):
if model._meta.app_label in self.route_app_labels:
return 'default'
return None
def allow_relation(self, obj1, obj2, **hints):
if (
obj1._meta.app_label in self.route_app_labels or
obj2._meta.app_label in self.route_app_labels
):
return True
return None
def allow_migrate(self, db, app_label, model_name=None, **hints):
if app_label in self.route_app_labels:
return db == 'default'
return None
class Measure:
route_app_labels = {'measure'}
def db_for_read(self, model, **hints):
if model._meta.app_label in self.route_app_labels:
return 'measure_db'
return None
def db_for_write(self, model, **hints):
if model._meta.app_label in self.route_app_labels:
return 'measure_db'
return None
def allow_migrate(self, db, app_label, model_name=None, **hints):
if app_label in self.route_app_labels:
return db == 'measure_db'
return None
then I created a new app measure
and in its models.py
I add this table
class SurveillanceDesPuits(models.Model):
PUITS= models.CharField(max_length=15)
....
.....
....
post_date = models.DateTimeField(default=timezone.now)
def __str__(self):
return self.PUITS
I saved in the admin.py
then I run :
> python manage.py makemigrations
> python manage.py migrate --database=measure_db
so the same issue it creates a new database named measure.db but it creates all tables in the first database+ the new table named SurveillanceDesPuits
?
could I delete those tables manually or there are another tips and tricks?
All my best