On Sat, Feb 5, 2011 at 6:29 AM, Daniel Klein <bri...@gmail.com> wrote:
> I'm teaching myself django by idling poking at a project in the hope that it
> may become a playable browser game some day. It's early days.
> So I have a model "Game", which is one game session. Each game is supposed
> to have two teams. I doubt the content of the Team model matters very much,
> so I'll just paste the relevant bits from Game:
>
> class Game(models.Model):
>
> team1 = models.OneToOneField(Team, related_name="team1", null=True)
>
> team2 = models.OneToOneField(Team, related_name="team2", null=True)
>
>
>
> def __init__(self, *args, **kwargs):
>
> super(Game, self).__init__(self, *args, **kwargs)
> self.team1 = Team()
> self.team2 = Team()
>

Don't do this! __init__ is called every time an object is created,
whether it is an existing instance loaded from the database, or a new
one created and not yet saved. This would clobber any saved team{1,2}
links with empty, new objects for every single instance of this class,
which is not what you want.

Your actual error comes because you cannot assign unsaved instances to
be related to another object. Just leave them as is, and do away with
the __init__ entirely.

Cheers

Tom

-- 
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 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to