Hello all !I posted my problem on stackoverflow but I didn't get any answer. I think this is something really simple. I just need to fullfill my database via a ManyToMany field... Why doesn't it work ? I don't know, but I am desperate...I give you the link : http://stackoverflow.com/questions/12918651/adding-values-in-my-database-via-a-manytomany-relationship-represented-in-adminIt would be fan-ta-stic if you would know the answer...Here is the copy/paste :
class RunHasSample(models.Model):
id = models.AutoField(primary_key=True)
run = models.ForeignKey(Run)
sample = models.ForeignKey(Sample)
dna_quantification_ng_per_ul = models.FloatField(null=True, blank=True)
lines = models.ManyToManyField(Line)
def __unicode__(self):
return u"Sample %s from run %s" % (self.sample, self.run)
In [10]: run1= Run(project=Project.objects.get(pk=1), sequencing_type=SequencingType.objects.get(pk=1))In [11]: run1.save()In [12]: Run.objects.all()Out[12]: [<Run: run started None from the project test project>]In [13]: s1=RunHasSample(run=run1, sample=Sample.objects.get(pk=1), dna_quantification_ng_per_ul=1)In [14]: s1.save()In [15]: run1.lines.add(s1)---------------------------------------------------------------------------AttributeError Traceback (most recent call last)/opt/scyld/python/2.6.5/lib/python2.6/site-packages/django/core/management/commands/shell.pyc in <module>()----> 1 run1.lines.add(s1)AttributeError: 'Run' object has no attribute 'lines'In [16]: run1.lines.add(s1)
In [10]: run1= Run(project=Project.objects.get(pk=1), sequencing_type=SequencingType.objects.get(pk=1))
In [11]: run1.save()
In [12]: Run.objects.all()
Out[12]: [<Run: run started None from the project test project>]
In [13]: s1=RunHasSample(run=run1, sample=Sample.objects.get(pk=1), dna_quantification_ng_per_ul=1)
In [14]: s1.save()
I found where the problem is... It is not a problem in the ManyToManyField but in the intermediate table. Django refused that my intermediate table doesn't have an unique id ! So, in the sql which created django, it created automatically an unique id named "id", but in my database I didn't create one (because the couple of two foreign key is usually enough). Next time, I'll be more carefull. |