简体   繁体   中英

Django ManyToManyField not present in Sqlite3

I'm new to Django and I have some issues with a ManyToMany relationship. I work on a blastn automatisation and here are my classes:

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

In a view, I want to access to Annotation's data from the rest of the model via this many to many field and then apply filters based on a form. But when I do a syncdb , the "sequence" field of the Blast class disappear :

In Sqlite3 :

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

So I can't load data in this table as I want. I don't understand why this field disappear during the syncdb. How can I do to link the first class to the others (and then be able to merge data in a template) ?

对于多对多关系,将创建一个中间联接表,请参见此处的文档: https : //docs.djangoproject.com/en/1.2/ref/models/fields/#id1

A ManyToManyField isn't itself a column in the database. It's represented only by an element in the joining table, which you have here defined explicitly as AnnotBlast (note that since you're not defining any extra fields on the relationship, you didn't actually need to define a through table - Django would have done it automatically if you hadn't).

So to add data to your models, you add data to the AnnotBlast table pointing at the relevant Blast and Annotation rows.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM