Django - 如何通过中介链接到遗留数据库? [英] Django - How to link to a legacy database via intermediary?
问题描述
英文 - 我有一个组织(Django),它指向许多项目)。但是所有的项目都不是指该组织。我有一个第三个表ProjectMap是通过一个触发器来构建的。它对应于一个项目的Organization.name。
如何粘合在一起,以便我这样做。
projects = Organization.objects.get(pk = 1).projects.all()
它不会得到所有项目只是在第三个表中匹配的项目。这是我到目前为止..
顺便说一句,如果有一个更好的策略,我都耳朵
类组织(models.Model):
name = models.CharField(max_length = 32)
projects = models.ManyToManyField (项目)
类项目(models.Model):
这是项目信息页面
注意:'id'确实存在,是$ p
result_number = models.IntegerField(null = True,db_column ='LBLDGRUNNO',blank = True)
building_number = models.IntegerField(db_column ='LBLDGNO' )
name = models.CharField(max_length = 150,db_column ='SPIBLGNAME',blank = True)
class Meta:
db_table = u'PROJINFO'
managed = False
class ProjectMap(models.Model):
projinfo_table_id = models.IntegerField(null = True)项目
的名称'id'$ name = models.CharField(max_length = 128,null = True)#'name'在组织$ b $中b
非常感谢!
不知道这是你的问题,但是您可以在ManyToManyField上使用通过
调用来定义一个中间表:
类组织(models.Model):
name = models.CharField(max_length = 32)
projects = models.ManyToManyField ,through =ProjectOrganisation)
class Project(models.Model):
#Stuff Here
class ProjectOrganisation(models.Model):
project = models.ForeignKey(Project)
organization = models.ForeignKey(Organization)
#其他字段
Django无论如何都会自动使用manytomany字段,只要你想添加额外的字段,这是这样做的。
I have to integrate a legacy design with my Django project and I am looking for some advice on using an intermediary. The existing design works but now I need to filter the Project by a third table.
In english - I have a Organization (Django) and which points to many Projects (Legacy). But all of the Project don't refer to that Organization. I have a third table ProjectMap which was build via a Trigger to address that. It corresponds the Organization.name to a project.
How do I glue this together in order allow me to do this.
projects = Organization.objects.get(pk=1).projects.all()
And it won't get ALL of the projects just the ones which match in the third table. Here is what I have so far..
By the way if anyone has a better strategy I'm all ears
class Organization(models.Model):
name = models.CharField(max_length=32)
projects = models.ManyToManyField(Project)
class Project(models.Model):
"""This is the project info page..
Note: 'id' does exist and is the pk.
"""
result_number = models.IntegerField(null=True, db_column='LBLDGRUNNO', blank=True)
building_number = models.IntegerField(db_column='LBLDGNO')
name = models.CharField(max_length=150, db_column='SPIBLGNAME', blank=True)
class Meta:
db_table = u'PROJINFO'
managed = False
class ProjectMap(models.Model):
projinfo_table_id = models.IntegerField(null=True) # 'id' of Project
name = models.CharField(max_length=128, null=True) # 'name' in Organization
Thanks so much!
Not sure if this is what your asking, but you can use the through
call on the ManyToManyField to define an intermediate table:
class Organization(models.Model):
name = models.CharField(max_length=32)
projects = models.ManyToManyField(Project, through="ProjectOrganisation")
class Project(models.Model):
#Stuff Here
class ProjectOrganisation(models.Model):
project = models.ForeignKey(Project)
organization = models.ForeignKey(Organization)
#Other Fields Here
Django does this automatically with manytomany fields anyway, just if you want to add extra fields, this is the way to do it.
这篇关于Django - 如何通过中介链接到遗留数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!