В моем django_project много приложений. Один из них - user_interaction. Я также использую 2 базы данных (Sqlite и MySQL). Я хочу, чтобы мое приложение user_interaction завершило работу с базой данных MySQL, тогда как другие приложения должны работать с sqlite3. Дело в том, что я хочу, чтобы все операции чтения, записи, сохранения, аутентификации и т.д. c user_interaction происходили в MYSQL базе данных.
И чтение, запись, сохранение, аутентификация и т.д. c других приложения происходят в 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 })