actually, I understand how it works, it seems to be a bug anyway. consider this:
from django.db import models class Author(models.Model): name = models.CharField(maxlength=50) class Reference(models.Model): """ >>> a = Author(name='some name') >>> a.save() >>> r = Reference(title='some title') >>> r.authors.add(a) >>> r.save() """ id = models.CharField(maxlength=5, primary_key=True) title = models.CharField(maxlength=50) authors = models.ManyToManyField(Author) I specified primary_key explicitly and it works without saving before adding Author. but if you'll change id field to id = models.AutoField(primary_key=True) or to id = models.IntegerField(primary_key=True) it doesn't. weird isn't it? I tried both on django-0.96 and svn version best Maxim Loginov http://zeliboba.by.ru On Sep 28, 10:02 pm, Leo <[EMAIL PROTECTED]> wrote: > Django's beautiful abstraction of object-orientation is leading you > astray... here's the test that will work (note the earlier added > r.save()): > > from django.db import models > > class Author(models.Model): > name = models.CharField(maxlength=50) > > class Reference(models.Model): > """ > >>> a = Author(name='some name') > >>> a.save() > >>> r = Reference(title='some title') > >>> r.save() > >>> r.authors.add(a) > >>> r.save() > """ > title = models.CharField(maxlength=50) > authors = models.ManyToManyField(Author) > > Why does this make it work? It actually makes sense if you know about > what's happening under the hood in the relational model: the > ManyToManyField is actually a separate table that contains the primary > keys for the Author and Reference. In order to correctly save that > separate table, we have to know the primary key for the Reference. > But, we can't know that until after we've saved the Reference the > first time. so the first r.save() saves the reference record itself, > thereby assigning it's primary key. The second r.save() actually is > only saving the ManyToManyField into its' separate table. > --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---