0

I'm a beginner in Django.

I would like to know how to configure two different projects to share the same database table in Django. The database I'm using is MySQL.

Let's say there's two projects, project A and project B. In project A, I already created a custom user model in it. And in project B, I want to use the custom user table from database created in project A just for the login. Means that in project A, there's registration and login for the custom user model.

In project B, there's login only that will be using the same custom user data in the database.

Do I have to redefine the same custom user model from project A in project B? What are the configuration settings that need to be done?

Thanks in advance.

azril
  • 5
  • 1
  • 3
  • Did this answer to your question https://stackoverflow.com/a/46233622/14457833 – Ankit Tiwari Oct 15 '21 at 08:31
  • I tried by using `python manage.py inspectdb > models.py` and put the file into project B, migration and all is good and database is connected. But somehow the project B cannot authenticate the user that created from project A. The return result from `authenticate()` is None, indicate that there's no data from custom user table. How to solve this? – azril Oct 18 '21 at 01:51

2 Answers2

0

project B can't accessible table from project A. but same database can accessible to multiple projects via enabling remote access on parent project database. but you should keep some point whenever your are made a model in any project then you must add that model in remain project

shiva gangula
  • 45
  • 1
  • 8
0

Okay, somehow I managed to solve this.

Like the previous step, I did python manage.py inspectdb > models.py in project A which I already created custom user model that used for registration and login. Then, I copy that models.py file into project B and changed the custom user model generated by project A to the exact same steps when I created the custom user model in project A. Means that I did the CustomUserManager class as well as the other necessary things required when you create a custom user model(AbstractBaseUser).

Then, in the settings.py of project B, I did AUTH_USER_MODEL which somehow solve this issue.

It seems like in order to use the same custom user model in different projects with same database, we need to redefine the exact custom user model along with managed = False.

azril
  • 5
  • 1
  • 3