Search code examples
djangomodels

Django - how to create duplicate apps without duplicating the code (models, signals and so on)


This may be a weird question, and from the title it may not be obvious, but couldn't find out a better / more descriptive title for my questions.

I have an inventory control app, with own models, signals, admin, and forms. I need to duplicate this app for multiple companies, and below is what I am currently doing:

app_shared
    models
        products
        adressbook
    forms
    signals
    admin

app_company1
    models
        inventory
        transactions
        ...
    forms
    signals
    admin

app_company2
    models
        inventory
        transactions
        ...
    forms
    signals
    admin

The issue is, between app_company1 and app_company2, everything is identical, same exact models, signals and so on. I have to keep them separate to create their own tables in database. But don't want to duplicate same code. Is there a way of preventing the duplication?


Solution

  • You should move all the identical code to the app_shared application and import it from app_company1 and app_company2.

    If have separate tables in the DB is a requirement, then the inventory and transaction models should be defined as abstract in app_shared. app_company1 and app_company2 should create their own corresponding models that derive from the abstract models. This will ensure separate tables are generated in the DB. See this documentation page for more details.