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

有帮助吗?

解决方案

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.

其他提示

For a many-to-many relationship an intermediate join table is created, see documentation here: https://docs.djangoproject.com/en/1.2/ref/models/fields/#id1

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top