我是Django的新手,我在ManyToMany关系方面遇到了一些问题。 我正在进行blastn自动化,这是我的课程:
class Annotation(models.Model):
sequence = models.IntegerField()
annotation = models.TextField()
start = models.IntegerField()
end = models.IntegerField()
class Blast(models.Model):
sequence = models.ManyToManyField(Annotation, through="AnnotBlast")
expectValue = models.IntegerField()
class AnnotBlast(models.Model):
id_blast = models.ForeignKey(Blast, to_field="id")
id_annot = models.ForeignKey(Annotation, to_field="id")
class Hit(models.Model):
id_hit = models.ForeignKey(Blast, to_field="id")
length = models.IntegerField()
evalue = models.IntegerField()
start_seq = models.IntegerField()
end_seq = models.IntegerField()
在视图中,我想通过多个到多个字段从模型的其余部分访问Annotation的数据,然后根据表单应用过滤器。但当我执行syncdb
时,Blast类的“序列”字段消失:
在Sqlite3中:
.schema myApp_blast
CREATE TABLE "myApp_blast" (
"id" integer not null primary key,
"expectValue" integer not null
);
所以我不能按照我的意愿在这个表中加载数据。我不明白为什么这个字段在syncdb期间消失了。如何将第一个类链接到其他类(然后能够合并模板中的数据)?
答案 0 :(得分:0)
对于多对多关系,会创建一个中间联接表,请参阅此处的文档:https://docs.djangoproject.com/en/1.2/ref/models/fields/#id1
答案 1 :(得分:0)
ManyToManyField本身不是数据库中的列。它仅由连接表中的元素表示,您在此处明确定义为AnnotBlast(请注意,由于您没有在关系上定义任何额外字段,因此您实际上不需要定义直通表 - Django会有如果你没有自动完成它。
因此,要向模型中添加数据,可以将数据添加到指向相关Blast和Annotation行的AnnotBlast表中。