我正在制作一个带插件的 Django 网站。每个插件都是一个简单的 django 应用程序,依赖于一个主要插件(甚至其他插件)。
虽然应用程序/插件之间的依赖性对我来说很清楚,但通过猴子修补添加列(作为插件特定模型的外键)以避免主应用程序依赖于插件应该是可以接受的。
由于主应用程序已经有南管理,所有插件也都有,我无法在这些模块的设置中更改迁移目录。
那么,我如何从另一个南方应用程序中猴子修补南方应用程序模型?
ps:我是法国人,如果您发现任何错误,请随时纠正我的问题,或者如果我不清楚,请随时提问。
编辑:我添加了一个关于我现在如何处理 Django 迁移的答案。
最佳答案
目前我最好的解决方案是在插件中制作我自己的迁移文件(这意味着在迁移文件的模型字典中添加表)。
如果所有模型都会自动跟随,我会在下次迁移时看到。
在我的新迁移文件中:
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 = {}
在我的模型文件中:
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')
关于python - 如何从插件中猴子修补南方处理的模型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16710310/