I have 开发者_StackOverflowa question about Django's namespace. Firstly, I implemented my own authentication system so I can have features like inline site editing for authenticated users. Bluntly put, I just didn't like the default admin setup.
Consequently, I have consciously not included any of the authentication middleware or admin features.
Anyway, I have a model called User
in vxd.auth.models
which I use in vxd.auth.control
to read the database for my system. When I run my authentication check I'm getting this error:
DatabaseError at /
column auth_user.first_name does not exist
LINE 1: SELECT "auth_user"."id", "auth_user"."username", "auth_user"...
Well of course it doesn't, my User
model doesn't implement a first name field. Django's, however, does.
This wasn't a problem until I upgraded Django and got hit by the CSRF middleware issue...
Relevant parts of settings.py:
TEMPLATE_LOADERS = ( 'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
)
MIDDLEWARE_CLASSES = (
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
)
AUTHENTICATION_BACKENDS = ('')
TEMPLATE_CONTEXT_PROCESSORS = ( "vxd.auth.contexts.Authentication", )
INSTALLED_APPS = (
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'reversetag',
'markitup',
'vxd.auth',
'Testbed.authadmin',
'Testbed.testapp',
)
If it's relevant, running Django 1.2.3 on Fedora.
As I said, this worked fine previously. I am under the strong suspicion that the django User model is being included somewhere by default.
What's going on, anybody know? One solution is to rename vxd.auth
which I suspect would fix the issue.
Updates for various people's debug suggestions (thanks by the way!): * Yep, I've tried dropping the tables. I can even manipulate my object via the shell like so:
$ python manage.py shell
>>> from vxd.auth.models import *
>>> u = User.objects.get(username="admin")
>>> print u
User object
>>> u.blue
u'baa26f39c47dd222a04aa8123b141e62ef5e0cffa658207b0754f811e6444ab9'
>>>
So something is clearly sneaking in somewhere.
Edit: authentication context processor:
def Authentication(request):
if AuthenticationCheck(sess=request.session, timeofaction=datetime.datetime.now(), ipaddress=request.META['REMOTE_ADDR']) == True:
return dict({'username': request.session["username"]})
else:
return dict()
Authentication Check code:
def AuthenticationCheck(sess, timeofaction, ipaddress):
try:
user = sess.get("username", None)
if user is None:
return False
else:
pass
except MultiValueDictKeyError:
# not a session object
# or no such key exists.
return False
# some computations based on sess variables which are set when login happens
# these are non-conflicting keys unless sess["vxd-blue"] is used elsewhere...
# Find "User" from DB.
print "User is " + user
#try: # don't catch so I can see the error
usr = User.objects.get(username=user) # this throws the exception shown.
Imports (contexts.py):
from datetime import date, datetime, timedelta
from vennarddjango.auth.control import *
imports (control.py):
from django.db import models
from django.contrib.sessions.models import *
from django.utils.datastructures import MultiValueDictKeyError
import hashlib
import datetime
from vxd.auth.models import *
from vxd.auth.openid.store import *
from vxd.auth.openid.methods import *
from vxd.utility.datetimefuncs import *
from vxd.utility.hashwrapper import *
You could edit the django
source, and drop into a pdb
session when django.contrib.auth.models
is imported, and follow the traceback up until you find the culprit. Pop the following at the top of that file.
import pdb; pdb.set_trace()
Then, at the prompt that appears, type tb
. (Or bt
, I can never remember which).
Did you syncdb
with contrib.auth
installed before creating vxd.auth
? It looks like your table structure might be out of date. If it's feasible, try dropping the auth_user
table and then re-syncing, and see if that fixes the issue.
I fixed it by refactoring vxd.auth
to vxd.myauth
. Whatever part of django was tripping up on requests to auth_user
now isn't. Not massively impressed with the namespace pollution, but never mind.
精彩评论