将整个应用程序映射到 Django 中的不同数据库

Mapping entire app to a different database in Django

我的 django_project 中有很多应用程序。 其中之一是 user_interaction。 我也在使用 2 个数据库。(Sqlite 和 MySQL) 我希望我的 user_interaction 应用程序在 MySQL database.whereas 上完成工作,其他应用程序应该在 sqlite3 上工作。 事情是我希望 user_interaction 的所有读取、写入、保存、验证等都发生在 MYSQL 数据库中。

以及其他应用程序的读取、写入、保存、验证等在 Sqlite 中发生。

这是行不通的。 那我该怎么办?

settings.py 文件(数据库部分)

DATABASE_APPS_MAPPING = {'user_interaction': 'user_interaction_db',
                        }


DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    },


    'user_interaction_db' :{
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'FMS_Database',
        'USER': '#####',
        'PASSWORD': '#####',
        'HOST': 'localhost',
        'PORT': '3306',
        'OPTIONS': {
            # Tell MySQLdb to connect with 'utf8mb4' character set
            'charset': 'utf8mb4',
        },
    },
    
}

Models.py 共 user_interaction:

class StudentAccountManager(BaseUserManager):
 

    def create_user(self,username,password=None):
        # if not email:
        #     raise ValueError("Users must have an email address")
        
        if not username:
            raise ValueError("Users must have an username")

        user = self.model(
            username = username,
        )

        user.set_password(password)
        user.save(using=self._db)
        return user

    def create_superuser(self,username,password):
        
        user = self.create_user(
            password=password,
            username = username,

        )

        user.is_admin = True
        user.is_staff = True
        user.is_superuser = True
        user.save(using=self._db)
        return user   



class StudentAccount(AbstractBaseUser):
    username    =  models.CharField(max_length=50,unique=True)
    email       =  models.EmailField(max_length=254,unique=True)
    joined_date =  models.DateTimeField(auto_now_add=True)
    last_login  =  models.DateTimeField(auto_now=True)
    is_admin    =  models.BooleanField(default=False)
    is_active   =  models.BooleanField(default=True)
    is_superuser = models.BooleanField(default=False)
    is_staff    =  models.BooleanField(default=False)
    

    management_name = models.CharField(max_length=254)
    full_name = models.CharField(max_length=254,null=True)
    contact_no = models.CharField(max_length=10)
    department = models.CharField(max_length=254)
    residential_status = models.CharField(max_length=254)
    batch_year = models.CharField(max_length=4,default='Null')
    entry_qrcode = models.ImageField(upload_to = qrcode_directory_path,null=True)  
    

    USERNAME_FIELD = 'username'

    #specifying manager
    objects = StudentAccountManager()


    def __str__(self):
        return self.username
    
    def has_perm(self,perm,obj=None):
        return self.is_admin
    
    def has_module_perms(self,app_label):
        return True

Views.py 共 user_interaction:

    def student_registration(request):
        
        if request.user.is_authenticated:
            return redirect('/useraccount/studentuser/')
        else:
        
            form = raw_user_form()
            if request.method == "POST" :
                form = raw_user_form(request.POST)
    
    
                if form.is_valid():
                    save_obj = StudentAccount()
                    save_obj.full_name = form.cleaned_data['full_name']
                    save_obj.username = form.cleaned_data['roll_no']
                    save_obj.management_name = form.cleaned_data['management_name']
                    save_obj.contact_no = form.cleaned_data['contact_no']
                    save_obj.department = form.cleaned_data['department']
                    save_obj.residential_status = form.cleaned_data['residential_status']
                    save_obj.email = form.cleaned_data['email']
                    save_obj.batch_year = form.cleaned_data['batch_year']
                    save_obj.set_password(form.cleaned_data['password'])
                    save_obj.save()
                    return redirect('/useraccount/thankyoupage/')
        
        return render(request,'user_interaction/student_registration_page.html',{'user_form': form })

def student_login(request):
    incorrect = ''
    if request.user.is_authenticated:
        return redirect('/useraccount/studentuser/')
    else:
        if request.method =='POST':
            username = request.POST.get('username')
            password = request.POST.get('pass')

            user = authenticate(request, username=username,password=password)
        
            if user is not None:
                login(request,user)
                return redirect('/useraccount/studentuser/')
            else:
                incorrect = 'Incorrect username or password'

    return render(request,'user_interaction/student_login_form.html',{ 'message' : incorrect })

您需要的是一个 Django 数据库路由器。查看示例 here.

假设您有这样的数据库:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    },


    'user_interaction_db' :{
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'FMS_Database',
        'USER': '#####',
        'PASSWORD': '#####',
        'HOST': 'localhost',
        'PORT': '3306',
        'OPTIONS': {
            # Tell MySQLdb to connect with 'utf8mb4' character set
            'charset': 'utf8mb4',
        },
    },
    
}

首先创建一个数据库路由器:

# db_routers.py
class AuthRouter:
    """
    A router to control all database operations on models in the
    auth and contenttypes applications.
    """
    route_app_labels = {'user_interaction',}

    def db_for_read(self, model, **hints):
        """
        Attempts to read auth and contenttypes models go to auth_db.
        """
        if model._meta.app_label in self.route_app_labels:
            return 'user_interaction_db'
        return None

    def db_for_write(self, model, **hints):
        """
        Attempts to write auth and contenttypes models go to auth_db.
        """
        if model._meta.app_label in self.route_app_labels:
            return 'user_interaction_db'
        return None

    def allow_relation(self, obj1, obj2, **hints):
        """
        Allow relations if a model in the auth or contenttypes apps is
        involved.
        """
        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):
        """
        Make sure the auth and contenttypes apps only appear in the
        'auth_db' database.
        """
        if app_label in self.route_app_labels:
            return db == 'user_interaction_db'
        return None

这样,user_interaction 模型的所有操作都通过 user_interaction_db

完成

接下来,将您的路由器添加到 settings.py

DATABASE_ROUTERS = ['path.to.AuthRouter',]