Saturday, February 5, 2011

Re: Having trouble creating instances of another model in the __init__ of one model

The error in save, not in constructor, seems you need to set dome default value to Team instances, not quite sure

On Sat, Feb 5, 2011 at 2:29 PM, Daniel Klein <bringa@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()

The code I used to have, which worked, manually created a game and its two teams. This was in the view for the "create new game" page, which I found all kinds of ugly. Since every game should start out with exactly two teams in it, it makes most sense to have that code inside the Game model, right?

So here's the error I'm currently getting when I try to save a game:

>>> from game.models import Team, Game
>>> tg = Game(name="testgame")
>>> tg.save()
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "c:\coding\django-1.1.1\django\db\models\base.py", line 410, in save
    self.save_base(force_insert=force_insert, force_update=force_update)
  File "c:\coding\django-1.1.1\django\db\models\base.py", line 470, in save_base
    manager.filter(pk=pk_val).extra(select={'a': 1}).values('a').order_by())):
  File "c:\coding\django-1.1.1\django\db\models\manager.py", line 129, in filter
    return self.get_query_set().filter(*args, **kwargs)
  File "c:\coding\django-1.1.1\django\db\models\query.py", line 498, in filter
    return self._filter_or_exclude(False, *args, **kwargs)
  File "c:\coding\django-1.1.1\django\db\models\query.py", line 516, in _filter_or_exclude
    clone.query.add_q(Q(*args, **kwargs))
  File "c:\coding\django-1.1.1\django\db\models\sql\query.py", line 1675, in add_q
    can_reuse=used_aliases)
  File "c:\coding\django-1.1.1\django\db\models\sql\query.py", line 1614, in add_filter
    connector)
  File "c:\coding\django-1.1.1\django\db\models\sql\where.py", line 56, in add
    obj, params = obj.process(lookup_type, value)
  File "c:\coding\django-1.1.1\django\db\models\sql\where.py", line 269, in process
    params = self.field.get_db_prep_lookup(lookup_type, value)
  File "c:\coding\django-1.1.1\django\db\models\fields\__init__.py", line 210, in get_db_prep_lookup
    return [self.get_db_prep_value(value)]
  File "c:\coding\django-1.1.1\django\db\models\fields\__init__.py", line 361, in get_db_prep_value
    return int(value)
TypeError: int() argument must be a string or a number, not 'Game'
 
I have no idea what this error means, and I strongly suspect I'm Doing It Wrong [tm] in the first place. There's probably a very easy way to do this that I'm not seeing.


Thanks!

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



--
===========================
Regards
Ronghui Yu

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

No comments:

Post a Comment