Opened 16 years ago

Closed 16 years ago

#7736 closed (invalid)

Differences in loading of applications between development server and mod_python

Reported by: Petr Marhoun <petr.marhoun@…> Owned by: nobody
Component: Core (Other) Version: dev
Severity: Keywords:
Cc: Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

I create simple module for demonstration of the problem - this module is called "servers". Its file settings.py:

DATABASE_ENGINE = 'sqlite3'
DATABASE_NAME = 'servers.db'
INSTALLED_APPS = 'django.contrib.contenttypes', 'django.contrib.auth', 'servers',
ROOT_URLCONF = 'servers.urls'

In models.py I add new field for User model:

from django.contrib.auth.models import User
from django.db import models

User.add_to_class('language', models.CharField(max_length=255, default='en'))

In urls.py I use this new field:

from django.conf.urls.defaults import *
from django.contrib.auth.models import User
from django.http import HttpResponse

field = User._meta.get_field('language')

def index(request):
    return HttpResponse(field, 'text/plain')

urlpatterns = patterns('',
    url(r'^servers/$', index),
)

With development server it works as I expect:

<django.db.models.fields.CharField object at 0xec0590>

But with mod_python there is an error:

ImproperlyConfigured: Error while importing URLconf 'servers.urls': User has no field named 'language'

But problem disappears if I add to the django.core.handlers.modpython.ModPythonHandler.call:

        # Load all applications.
        from django.db.models import get_apps
        get_apps()

So it seems that not all applications are loaded with mod_python.

Attachments (1)

00-modpython-fix.diff (561 bytes ) - added by Petr Marhoun <petr.marhoun@…> 16 years ago.

Download all attachments as: .zip

Change History (2)

by Petr Marhoun <petr.marhoun@…>, 16 years ago

Attachment: 00-modpython-fix.diff added

comment:1 by Malcolm Tredinnick, 16 years ago

Resolution: invalid
Status: newclosed

I don't see this as a bug. Yes, there's a difference, but that's neither here nor there. The assumption that all apps will be transparently loaded immediately is the error here. It happens to do that with the dev server, but it's not something that is indicated as "must happen". The get_apps() function is called when required, but not earlier. Forcing it to be called at arbitrary points in the pipeline is inconsistent and not worth it.

If you want your code fragment to work, which depends upon the "server" module being loaded, then make sure you explicitly import the "server" module. It will make your code cleaner ("explicit is better than implicit" and all that) and avoid dependencies on the timing of any particular piece of lazy loading.

Note: See TracTickets for help on using tickets.
Back to Top