Re: Problems with __str__ methods in my Models

2006-07-13 Thread Kenneth Gonsalves


On 14-Jul-06, at 12:01 AM, Jason Murray wrote:

> TypeError: int argument required

put int(value) around all your int values, or refer to int values by % 
s and put a str() around them

-- 

regards
kg
http://lawgon.livejournal.com
http://avsap.org.in



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



Re: Problems with __str__ methods in my Models

2006-07-13 Thread Adrian Holovaty

On 7/13/06, Jason Murray <[EMAIL PROTECTED]> wrote:
>File "/home/jmurray/prfa/../prfa/standings/models.py", line 88, in __str__
>  return "%s: %i (%i)" % (self.game.datetime.strftime("%Y-%m-%d"),
> self.game.away.number, self.away_runs)
> TypeError: int argument required

This is happening because at least one of your Result objects has an
away_runs field with a value of None. The "%i" string formatting
parameter accepts only integers; if you feed it None, you'll get that
exact error. Try this at the Python interactive prompt:

print '%i' % None

The solution is to either use '%s' instead of '%i', or to special-case
the case in which self.away_runs is None. Example:

def __str__(self):
if self.away_runs is None:
away_txt = 'No away runs'
else:
away_txt = str(self.away_runs)
return "%s: %i (%s)" % (self.game.datetime.strftime("%Y-%m-%d"),
self.game.away.number, away_txt)

Adrian

-- 
Adrian Holovaty
holovaty.com | djangoproject.com

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



Problems with __str__ methods in my Models

2006-07-13 Thread Jason Murray

Yes it's me again :)

Anyway I've populating my Models with __str_ methods. I've only had on 
hicough.

Here is the model in question:
class Result(models.Model):
 ID = models.IntegerField(primary_key=True)
 home_runs = models.IntegerField("home team runs", null=True, blank=True)
 away_runs = models.IntegerField("visiting team runs", null=True, 
blank=True)
 game = models.ForeignKey(Game, db_column='game')
 ump1 = models.ForeignKey(Ump, related_name='result_ump1', 
db_column='ump1')
 ump2 = models.ForeignKey(Ump, related_name='result_ump2', 
db_column='ump2')
 def __str__(self):
 return "%s: %i (%i)" % (self.game.datetime.strftime("%Y-%m-%d"),
 self.game.away.number, self.away_runs)
 class Meta:
 db_table = 'result'
 class Admin:
 pass

When I jump into the shell (python manage.py shell) to try it out...

 >>> Result.objects.all()
Traceback (most recent call last):
   File "", line 1, in ?
   File 
"/usr/local/lib/python2.3/site-packages/Django-0.95-py2.3.egg/django/db/models/query.py",
 
line 88, in __repr__
 return repr(self._get_data())
   File 
"/usr/local/lib/python2.3/site-packages/Django-0.95-py2.3.egg/django/db/models/base.py",
 
line 76, in __repr__
 return '<%s: %s>' % (self.__class__.__name__, self)
   File "/home/jmurray/prfa/../prfa/standings/models.py", line 88, in __str__
 return "%s: %i (%i)" % (self.game.datetime.strftime("%Y-%m-%d"), 
self.game.away.number, self.away_runs)
TypeError: int argument required


However when I retrieve an individual result...

 >>> r=Result.objects.get(pk=234)
 >>> r

 >>>

This is confusing to me. __str__ works for an individual objects, but not 
a collection of them. What's happening here?

I'm puzzled, I hope someone here is able to help me out.

BTW it is self.away_runs that causes the problems. When I remove that from 
the __str__ function it works just fine.

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