[Django]-Shared models between two Django projects and ForeignKey to a model that exists only in one of them

5👍

Specifically, I need to retain the functionality of querying both ways in the first project, e.g., fk__some_a_field and b_set.

If you want to use django orm, you would have to recreate your A model from project 1 in project 2. But as model A is managed by the project 1, consider adding next lines to your model in project 2:

class A(models.Model):
    ...
    
    class Meta:
        managed = False
        db_table = 'your database table name where A model is stored'

managed=False would tell django to ignore migrations for it, and django won’t be allowed to change that model’s database table.

The other solution if you don’t want to duplicate models from project 1 to project 2, is to not use django orm. And write sql queries by yourself. But as you mentioned you don’t want to do this

P.S if you don’t know how to see name of database table for model A, you can output it like this: a_model_instance._meta.db_table, or look it in some tools like pgadming if you are using postgres

👤V.D.

Leave a comment