I'm currently writing some doctests for a model and got the following error message which I don't understand:
Failed example:
print q
Exception raised:
Traceback (most recent call last):
File "/usr/local/lib/python2.4/site-packages/django/test/doctest.py", line 1243, in __run
compileflags, 1) in test.globs
File "<doctest ais.core.models.Quittung[11]>", line 1, in ?
print q
File "/home/dirk/django/projects/ais/core/models.py", line 674, in __str__
return _('Quittung for User %s on MetaObjekt %s, seen: %s, ack: %s') \
TypeError: 'bool' object is not callable
Here is my simplified model:
class Quittung(models.Model):
"""
>>> mo = MetaObjekt(titel='MetaObjekt 2')
>>> mo.save()
>>> u = User(username='User 2',email='us...@test.de',password='userpass')
>>> u.save()
>>> q = Quittung(metaobjekt=mo,user=u)
>>> q.save()
>>> print q
"""
metaobjekt = models.ForeignKey(MetaObjekt, related_name='quittungen', editable=False)
user = models.ForeignKey(User, related_name='quittungen', editable=False)
gesehen = models.BooleanField('gesehen', db_index=True, default=False)
quittiert = models.BooleanField('quittiert', db_index=True, default=False)
def __str__(self):
import pdb; pdb.set_trace()
return _('Quittung for User %s on MetaObjekt %s, seen: %s, ack: %s') \
% (self.user.username, self.metaobjekt.titel, self.gesehen, self.quittiert)
As you can see I added the Python-Debugger and printing type(_) result in bool. Shouldn't this be a function ?
The __str__()-function is working fine in the real application.
Regards,
Dirk
--
GMX DSL-Flatrate 0,- Euro* - Überall, wo DSL verfügbar ist!
NEU: Jetzt bis zu 16.000 kBit/s! http://www.gmx.net/de/go/dsl
I wonder if this is an unfortunate naming clash?
When running from the interactive prompt (including during doctest
runs), the variable '_' is used to store the most recent result. This
clashes with Django's internal use of _ as the name of a function. The
problem here is that instead of requiring _() to be aliased inside each
model, it was decided to stick this function into __builtins__. I wasn't
completely thrilled with that at the time, mostly for non-specific
reasons, and now we might have a reason to be concerned.
Needs more investigation to confirm if this is the case. Ine test would
be to explicitly import gettext() as _() in your test file, since that
would introduce different aliasing rules for the two cases, I believe.
Regards,
Malcolm
>
> Needs more investigation to confirm if this is the case. Ine test would
> be to explicitly import gettext() as _() in your test file, since that
> would introduce different aliasing rules for the two cases, I believe.
>
from django.utils.translation import gettext as _
worked fine for me.
Thanks,
Dirk
--
Der GMX SmartSurfer hilft bis zu 70% Ihrer Onlinekosten zu sparen!
Ideal für Modem und ISDN: http://www.gmx.net/de/go/smartsurfer
Yeah, so that kind of confirms my suspicion. Okay, another bug to fix
when we get time. Thanks for testing it. I've made ticket #2920 for this
so that we don't forget.
Regards,
Malcolm