Migration in different schemas of database in Python Django 5
I have a problem to make migrations in PostgreSQL database with 2 schemas: public and users. I have models for users and their profiles. It requires to put them on schema with name "users" and then create superuser and some basic users. But every time when I do migrations, Django creates tables in default schema "public" or I have different mistakes. Many attempts to fix this didn't get me a result, so I need help.
My models:
class MyUser(AbstractUser):
class Meta:
managed = True
db_table = 'users\".\"user' # gives a problem
email = models.EmailField(_("email address"), unique=True)
USERNAME_FIELD = "email"
REQUIRED_FIELDS = ["username"]
objects = MyUserManager()
class MyProfile(models.Model):
class Meta:
managed = True
db_table = 'users\".\"profile' # gives a problem
user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
name = models.CharField(max_length=200, default="Name")
surname = models.CharField(max_length=200, default="Surname")
My Manager:
class MyUserManager(BaseUserManager):
def create_user(self, email, password, **extra_fields):
if not email:
raise ValueError(_("Email required"))
email = self.normalize_email(email)
user = self.model(email=email, **extra_fields)
user.set_password(password)
user.save()
return user
def create_superuser(self, email, password, **extra_fields):
extra_fields.setdefault("is_staff", True)
extra_fields.setdefault("is_superuser", True)
extra_fields.setdefault("is_active", True)
if extra_fields.get("is_staff") is not True:
raise ValueError(_("is_staff=True required"))
if extra_fields.get("is_superuser") is not True:
raise ValueError(_("is_superuser=True required"))
return self.create_user(email, password, **extra_fields)
My DB:
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': "djangotest",
'USER': "postgres",
'PASSWORD': "********",
'HOST': "localhost",
'PORT': "5432",
'OPTIONS': {
'options': '-c search_path=public,users'
}
Maybe something I need to add to router:
ROUTED_MODELS_SCHEMA = [MyUser]
class DataBaseRouter(object):
def db_for_read(self, model, **hints):
if model in ROUTED_MODELS_SCHEMA:
return 'users'
return None
def db_for_write(self, model, **hints):
if model in ROUTED_MODELS_SCHEMA:
return 'users'
return None
If I type python manage.py migrate
, I have tables only in "public" schema. If I type python manage.py migrate
with --database
I have tables in "users" schema, but server complains about missing migration which must be already done, and I can't create superuser.
If I use db_table = 'users\".\"user'
I have syntax error and can't create any table.
I tried to find answer in different sites and changed different project files like dbrouter.py e.t.c. but this problem still exists. Please, give me some steps, what should I do. Or a link to a really working project in GitHub to analyze it. Thanks.