1

I am new to using Django framework. I want to use the same database(PostgreSQL) tables for 2 different projects. I was able to access the same database from two different projects. But how to access the tables?

Firstly, I have models in project1

class employees(models.Model): 
    employeeID = models.IntegerFiled()
    employeeName = models.CharField(max_length=100)

This would create a project1_employees table in the database. I want to access this table in project2. If I have the same model in the project2 and migrate it creates a new table project2_employees in the same database. These are two entirely different projects.

2 Answers 2

1

For the second project change the meta of your model this way:

class employees(models.Model): 
    employeeID = models.IntegerFiled()
    employeeName = models.CharField(max_length=100)
    class Meta:
        managed = False
        db_table = 'project1_employees'

And then make fake migrations:

python manage.py migrate --fake
Sign up to request clarification or add additional context in comments.

Comments

1

Use managed = False in the second project model class Meta:. This will prevent the migrate code from creating the table. For more information see:

https://docs.djangoproject.com/en/3.0/ref/models/options/#managed

What you need to consider is whether you think the projects will diverge enough they will end up needing separate databases/tables.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.