Django: extending models of other apps
<p>I'm working on small ERP-like project on Django which contains different apps (Products, Sales, Purchases, Accounting, MRP, ...). A few of them have dependencies (for example, a Sales app requires the Products app).</p>

<p>For the sake of modularity and loose-coupling, I'm trying to keep the apps as independent as possible. However, it would be extremely useful and make things way simpler if the apps could extend (add new fields) tables in the models of their dependencies (for instance, Sales would add a <code>can_be_sold</code> BooleanField in one of the tables within the scope of Products, if it is installed).</p>

<p>This way when a user internally chooses to install an app, the required changes in the database are made for it to integrate properly with the dependencies. The user installs only the apps he needs without having to provide unnecessary or unrelated information a priori in the dependencies.</p>

<p>I thought of making one-to-one relationships with the dependencies, but this solution doesn't convince me because (a) it doesn't seem very efficient to make so many new tables for maybe one or two extra fields, and (b) working with forms and signals would be a hassle (making code harder to mantain if many apps are installed and decreasing modularity overall). </p>

<p>Inheritance or abstract classes also seem inappropriate, because I'm not trying to create a model for, say, a subproduct, but rather grow or expand on existing information (entries) in a table.</p>

<p>What is the best way to achieve this? Should I be looking into writing custom migration operations? Otherwise, are there better approaches? Thanks!</p>

<p>I d recommend you to create a database tabel to save all your possible registered modules and class.
so you can use it as variable python code inside an eval statement.
and if you have pre-defined methods names, in this approach bellow i called it common_method.</p>

<p>you don t even need to have an abstract class</p>

<pre><code>my_evaluated_code = 'from '+mymodule_var_name + ' import ' + myclass_var_name +' as custommodule'
eval(my_evaluated_code)
custommodule.common_method()
</code></pre>

python django django-models django-migrations django-apps