Best way to combine custom Django project tables?

I have two Django projects on one server. The first one was launched a few months ago, and since then has collected hundreds of user accounts. The second project starts in a few days, and we would like the second project to allow users of the first application to authenticate using the same credentials.

At first, I was going to simply reset the user table from the first project to the second project, but this will not allow a synchronous solution (the user creates an account in project B, does not have access to project A).

Does Django have the ability to reassign database names (since they are on the same server) to authenticate users and then back to the original database after authentication is complete?

If not, what do you think would be the best solution for my problem? In addition, we use MySQL.

+4
source share
5 answers

If you wait a while, eventually Django will get support for multiple databases.

But now I believe that the best solution to your problem is to synchronize the two tables of the database users after the changes are made. You can use signal support for this:

from django.db import models def user_post_save(sender, instance, **kwargs): ... run script to synchronize tables ... models.signals.post_save.connect(user_post_save, sender=User) 

You cannot use ORM ... but dumping the original table and then deleting the destination and importing into it would be relatively painless. This can definitely cause synchronization problems, but transactions will basically solve this. If the two sites advancing on each other are worried, I can study the write lock setting in the User table during the update and set some kind of wait loop in the save () method of the user model (or the pre_save signal) to check lock before saving is completed. This ensures that the post_save signal will not be sent during synchronization.

+3
source

This will probably make me shoot; but you can create a user table of a new project simply as a representation of the first project:

 DROP TABLE proj2.users; CREATE VIEW proj2.users AS SELECT * FROM proj1.users; 
+2
source

Support for multiple databases is available.

+1
source

Django does not currently support multiple databases (there is wikipage about this question).

Another approach would be to write a user authentication user module for a second website that will execute SQL queries on the first website database to provide a login. When the user from the first site enters the second Django, he will create the user on the second site. But this can have some problems with changing the full name and email addresses, so it could really be used only to have a username and password. Also, your users will need to register on the first site in order to access the second, so you will need to configure registration for registration on the first more.

0
source

You can use multidb application to work with many db as you like

http://github.com/kron4eg/multidb-django/tree/master

0
source

All Articles