Model Loading Problem

27 views
Skip to first unread message

Collin Anderson

unread,
Nov 3, 2009, 2:33:12 PM11/3/09
to Django users
Hello All,

I am getting the following error on Django 1.1:

Mod_python error: "PythonHandler django.core.handlers.modpython"

Traceback (most recent call last):

File "/usr/lib/python2.4/site-packages/mod_python/apache.py", line
299, in HandlerDispatch
result = object(req)

File "django/core/handlers/modpython.py", line 228, in handler
return ModPythonHandler()(req)

File "django/core/handlers/modpython.py", line 201, in __call__
response = self.get_response(request)

File "django/core/handlers/base.py", line 73, in get_response
response = middleware_method(request)

File "project/order/middleware.py", line 15, in process_request
request.order = get_order(request)

File "project/order/middleware.py", line 10, in get_order
return Order()

File "django/db/models/base.py", line 307, in __init__
val = field.get_default()

File "django/db/models/fields/related.py", line 728, in get_default
if isinstance(field_default, self.rel.to):

TypeError: isinstance() arg 2 must be a class, type, or tuple of
classes and types



Basically I have two apps:

in order/models.py
class Order(models.Model):
account = models.ForeignKey('account.Account')

in account/models.py:
class Account(models.Mode):
name = models.CharField(max_length=100)

The TypeError is being thrown because self.rel.to is
"account.Account". At the point of the middleware processing, account/
models.py hasn't been imported yet, therefore the Account model has
not been registered, so self.rel.to has not yet been resolved to the
actual Account model.

This error does not happen in the shell or in a runserver, probably
because I know at least shell calls get_models() which forces all
models to be registered.

This seems related to http://code.djangoproject.com/ticket/1796.

Am I the only one who has ran into this problem? I can think of
several workarounds, but is there an actual solution to this problem,
or is this a bug in django? Shouldn't all models be registered when
mod_python starts up?

Thanks,
Collin Anderson
Reply all
Reply to author
Forward
0 new messages