2 つのアプリケーションを含む django プロジェクトがexperiments
ありmetadata
、1 つのビューでこれら両方のアプリケーションのモデルを操作する必要があるため、次のようになります。
from experiments.models import *
from metadata.models import *
両方のアプリケーションに同じ名前のモデルがあります。では、1 つのアプリで 2 つのモデルを使用するにはどうすればよいでしょうか?
from experiments import models as exp_models
from metadata import models as meta_models
foo = exp_models.Foo.objects.all()
bar = meta_models.Bar.objects.all()
ところで。from module import *
悪い習慣と見なされ、名前空間が乱雑になります。
あなたが持っていると仮定しexperiments/models.py
ます:
class Foo(models.Model):
name = models.CharField(max_length=200)
また、次のmetadata/models.py
ものもあります。
class Foo(models.Model):
name = models.CharField(max_length=200)
両方を使用する場合は、次のようにインポートします。
from metadata.models import Foo as meta_foo
from experiments.models import Foo as experiment_foo