Thanks a lot for your help Malcom. Malcolm Tredinnick escribió, El 18/08/2008 0:03: > > On Sun, 2008-08-17 at 23:51 +0200, MarC wrote: >> Hi again, >> I'm still fighting against it and reading related posts in the mailing list >> archive with no >> success... Now I just found it won't work either for a simpler example. >> Looks like I should be doing something really stupid but I can't see it... >> >> from django.db import models >> >> class Test2(models.Model): >> name = models.CharField(max_length=50); >> def __unicode__(self): >> return self.name >> >> class Test(models.Model): >> name = models.CharField(max_length=50); >> rel = models.ForeignKey(Test2); > >> def __unicode__(self): >> return self.name >> >> SHELL: >> >>> from trustworker.main.models import * >> >>> t2=Test2(name="sample"); >> >>> t=Test(name="example",rel=t2); >> >>> t2.save(); >> >>> t.save(); > > This is caused by an implementation detail, but the basic rule is "don't > use unsaved objects as related objects". > > What's going on is when you do the "rel=t2" part, the related field > attribute actually extracts the value it will be storing (the primary > key value of t2, in this case) and stores that. It doesn't store the > reference to t2 directly on the attribute (it does store the reference > internally for caching reasons, but that's beside the point here). > > Since t2 is unsaved, it's primary key attribute is None and that's why > the wheels fall off later. Saving t2 prior to using it to construct t > will almost certainly fix the problem. > > Yes, this is slightly odd and a little trappy. > > Regards, > Malcolm > > > > > >
--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---