Django models across multiple projects/microservices. How to?

后端 未结 4 2047
旧时难觅i
旧时难觅i 2021-02-19 10:34

I\'m wondering how to solve sharing of the model structure between multiple (separated) django projects/microservices. Eg:

  1. Project: API
  2. Project: Users das
4条回答
  •  南方客
    南方客 (楼主)
    2021-02-19 10:48

    Django models is just a Python module, of course Django does a lot of 'magic' in other modules (i.e. forms, admin) using introspection, but the models itself could be used separately if desired.

    So there's no problem... If you use different "Django projects" for each project you named (on same machine or different), then just make your models as separate "Django app" and set each project to use it. If each project named by you is "Django app" (in this case on same machine obviously) then just import your models module in each "project" and use it. Even if you don't use Django, say your "API project" is based on Flask, you can still import Django models in Flask modules.

    Behind the scene of Django models there's the database, which doesn't care from where it is queried, and Django just provides a convenient way to use the DB via Python classes. So if the models from different apps or projects are set to use the same DB, then they will be 'shared'.

提交回复
热议问题