Re: Adding values in my database via a ManyToMany relationship represented in admin.py

248 views
Skip to first unread message

smcoll

unread,
Oct 19, 2012, 3:49:45 PM10/19/12
to django...@googlegroups.com
i'm not clear about your models:
  • the admin inline refers to a RunHasSample model that i don't see in your models.py
  • the Sample model has an FK to Sample, but looks like the run_has_sample table.  Should i presume that's the RunHasSample intermediary model instead?
  • i don't see your tables for Run or Sample, which may not be relevant, but given my present confusion, might help
  • It seems like RunHasSample is an explicit intermediary between Run and Sample, and then there is also an intermediary table between RunHasSample and Line. That means your explicit m2m intermediary table has an implicit m2m to et another table.  Is that what you want?
i'm lost at that point.

Did you look over the m2m admin inlines section in the docs?  https://docs.djangoproject.com/en/1.4/ref/contrib/admin/#working-with-many-to-many-models  The usage of `model = Group.members.through` might be a hint, and then there's additional info on working with intermediary models as well.



On Friday, October 19, 2012 4:09:10 AM UTC-5, Louise OTT wrote:
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...


It would be fan-ta-stic if you would know the answer...

Here is the copy/paste :

Louise OTT

unread,
Oct 19, 2012, 4:09:03 PM10/19/12
to django...@googlegroups.com
Ow sorry !

I gave the wrong model in models.py. Don't look at the Sample table, it is not relevant to my problem. I have a run and I want to add it with its RunHasSample (every RunHasSample have a link to a sample but it is the user who choose the sample linked)


    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)

For the admin.py, you don't need anything else to see, exept maybe the print screen of the "add run". 

When I click on "save", it does the error I posted.

I need to clarify two things :
- First the sqlall is good, finaly, the foreign key constraint is created 
- And I tried to create the many to many relationship in the shell of django, and it didn't work either this way. I really think the problem comes from the fact that it doesn't find the "intermediate" table between table "line" and table "run_has_sample". 

Thank you for your time !
  
add_run.jpg

Louise OTT

unread,
Oct 19, 2012, 4:11:56 PM10/19/12
to django...@googlegroups.com
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)

smcoll

unread,
Oct 19, 2012, 5:32:47 PM10/19/12
to django...@googlegroups.com
It's true: the Run model does not have a "lines" attribute; the RunHasSample model does.  Also, `s1` is not a Line nor a Sample, it's an instance of the intermediary RunHasSample.  So you're not going to be able to directly add a Line instance to a Run instance- you'll have to add it to the relevant RunHasSample instance instead.

Louise OTT

unread,
Oct 22, 2012, 7:48:54 AM10/22/12
to django...@googlegroups.com
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 [30]: s1.lines.add(Line.objects.get(id=1), Line.objects.get(id=2))

In [31]: s1.save()


Yes, I added the lines to the wrong table :)
Now it is working in the shell. I'll try to use this to make this work too in the admin.py interface.

I keep you posted, thanks for your help !


Louise OTT

unread,
Oct 22, 2012, 8:13:21 AM10/22/12
to django...@googlegroups.com
Ok, it is still not working. 
Django can't find the intermediate table.

And I can't put an "inlines" into an "inlines" ! My RunHasSample are already in a field "inlines" in the RunAdmin, so I cannot put the lines in an "inlines" field into the class RunHasSamplesInLine


Louise OTT

unread,
Oct 22, 2012, 3:24:32 PM10/22/12
to django...@googlegroups.com

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.

Reply all
Reply to author
Forward
0 new messages