Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to monkey patch south handled models from plugin?

I'm making a django site with plugins. Each plugin is a simple django app depending on a main one (or even other plugins).

While dependency between applications/plugins are clear to me, it should be acceptable to add column (as foreign key to plugin specific models) via monkey patching to avoid the main app to depends on the plugin.

Since main application already have a south management and so have all plugins, I can't change migrations directory in settings for those modules.

So, How do I monkey patch a south application model from an other south application?

ps: I'm French, feel free to correct my question if you spot any error or to ask anything if I'm unclear.

Edit: I added an answer on how I do now on django migrations.

like image 490
christophe31 Avatar asked May 23 '13 09:05

christophe31


1 Answers

For now my best solution have been to make my own migration file in the plug-in (which implied add tables in the models dictionary of the migration file).

I'll see later on next migration if all models will follow automatically.

In my new migration file:

class Migration(SchemaMigration):
    def forwards(self, orm):
        db.add_column(u'remoteapp_model', 'fieldname',
                      self.gf('django.db.models.fields.related.ForeignKey',
                      (to=orm["my_plugin.MyModel"], default=None, null=True, blank=True),
                      keep_default=False)


    def backwards(self, orm):
        db.delete_column(u'remoteapp_model', 'fieldname')

    # for models, you may want to copy from a previous migration file
    # and add from the models of the main application the related tables
    models = {} 

In my models file:

from remoteapp.models import RemoteModel
from django.db import models

class MyModel(models.Model):
    pass

models.ForeignKey(MyModel, null=True, blank=True,
                  default=None).contribute_to_class(RemoteModel, 'my_model')
like image 103
christophe31 Avatar answered Sep 24 '22 20:09

christophe31