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.

Sorry if that was confusing, but I think it's pretty much a
requirement given the way the relational model works.  Admittedly,
there might be some circumstances where Django could "just know" to
internally cascade the save()s in the right order...

Leo

On Sep 27, 8:27 pm, zeliboba <[EMAIL PROTECTED]> wrote:
> hi again
>
> I'm writing custom scientific reference management application and
> encountered a strange thing, cannot understand if it is bug or
> feature, see example models.py:
>
> 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()
>     """
>     title = models.CharField(maxlength=50)
>     authors = models.ManyToManyField(Author)
>
> the above doctest failed with message "ValueError: <...> instance
> needs to have a primary key value before a many-to-many relationship
> can be used." ok, I gave it (add one more field to Reference, though
> it is default):
>
>     id = models.AutoField(primary_key=True)
>
> and it does't work! ok, try nondefault:
>
>     idid = models.IntegerField(primary_key=True)
>
> again doesn't work... the only way to make it work is to modify title
> field by adding primary_key=True (or crate a dummy CharField since I
> do not want to have primary key on title)
>
> I tried django-0.96 on Gentoo


--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to