2013-04-16 82 views
0

我是Django的新手,我遇到了ManyToMany關係的一些問題。 我在BLASTN automatisation工作,這裏有我的課:Django ManyToManyField在Sqlite3中不存在

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() 

在視圖中,我想通過這麼多從模型的其餘部分訪問註釋的數據很多字段,然後根據應用濾鏡形成。但是,當我做了syncdb,爆破類的「序列」字段消失

在SQLITE3:

.schema myApp_blast 
CREATE TABLE "myApp_blast" (
    "id" integer not null primary key, 
    "expectValue" integer not null 
); 

所以我不能在此表中,因爲我想加載數據。我不明白爲什麼這個字段在syncdb中消失。我怎樣才能將第一類鏈接到其他類(然後能夠合併模板中的數據)?

回答

0

一個ManyToManyField本身不是在數據庫中的列。它僅由連接表中的一個元素表示,您在此處將其明確定義爲AnnotBlast(請注意,因爲您沒有定義關係中的任何額外字段,所以實際上並不需要定義一個通過表 - Django會有如果你沒有自動完成)。

因此,要將數據添加到模型中,需要將數據添加到指向相關Blast和Annotation行的AnnotBlast表中。

+0

謝謝你,你的評論對我有幫助。我真的認爲ManyToManyField是數據庫中的一列...謝謝丹尼爾! –