我有两个Django应用程序,如下图所示。按原样,Django将创建两个数据库表,' appa_modela'和' appb_modelb'。 commA.py创建的ModelB对象将保存到' appb_modeb'表。但是,我希望appA创建的ModelB对象独立于appB创建的对象。我知道“Meta”#39;子类,其中' app_label'和' db_table'已设定。如何才能确保“app_label'被改为appA'何时将ModelB导入appA模块?
的appA / models.py
from django.db import models
class ModelA(models.Model):
list_of_modelB_pk = models.TextField(default='',
validators=[validate_comma_separated_integer_list])
的appA /管理/命令/ commA.py
from django.core.management.base import BaseCommand
import json
from appA.models import ModelA
from appB.models import ModelB
class Command(BaseCommand):
def handle(self, *args, **options):
mb1 = ModelB()
mb1.save()
print mb1._meta.app_label # prints 'appB' why not 'appA'?
mb2 = ModelB()
mb2.save()
ma = ModelA()
ma.list_of_modelB_pk = json.dumps([mb1.pk,mb2.pk])
ma.save()
print ma._meta.app_label # prints 'appA'
程序appB / models.py
from django.db import models
class ModelB(models.Model):
fieldB1 = models.IntegerField(default=0)
程序appB / manangement /命令/ commB.py
from django.core.management.base import BaseCommand
from appB.models import ModelB
class Command(BaseCommand):
def handle(self, *args, **options):
mb1 = ModelB()
mb1.save()
mb2 = ModelB()
mb2.save()