Django OnetoOneField的默认用户模型

时间:2014-11-14 17:30:06

标签: django

我正在尝试在User模型中为我使用OnetoOneField添加额外字段。 我想制作学生档案,但是它给出了错误。我的设置是

"""
Django settings for smvdu_portal project.
For more information on this file, see
https://docs.djangoproject.com/en/1.6/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.6/ref/settings/
"""

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.6/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '$9b^ttpvr66hxr0w6d*nep&=f5(hzdpmob)ng(a)yp*&sj1nsw'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

TEMPLATE_DEBUG = True

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = (
   'django.contrib.admin',
   'django.contrib.auth',
   'django.contrib.contenttypes',
   'django.contrib.sessions',
   'django.contrib.messages',
   'django.contrib.staticfiles',
   'django_extensions',
   'quiz',
   'student',
   'django_extensions'


   )

 MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
     )
 TEMPLATE_DIRS = (os.path.join(BASE_DIR, 'template'),
 # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
 # Always use forward slashes, even on Windows.
 # Don't forget to use absolute paths, not relative paths.
 )
 ROOT_URLCONF = 'smvdu_portal.urls'

WSGI_APPLICATION = 'smvdu_portal.wsgi.application'


# Database
# https://docs.djangoproject.com/en/1.6/ref/settings/#databases

DATABASES = {
'default': {
    'ENGINE': 'django.db.backends.sqlite3',
    'NAME': os.path.join(BASE_DIR, 'sqlite3.db'),
    'USER': '',
    'PASSWORD': '',
    'HOST': '',                      # Empty for localhost through domain sockets or     '127.0.0.1' for localhost through TCP.
    'PORT': '',  
}
}

# Internationalization 
# https://docs.djangoproject.com/en/1.6/topics/i18n/

TEMPLATE_CONTEXT_PROCESSORS = (
  "django.contrib.auth.context_processors.auth",
   "django.core.context_processors.debug",
   "django.core.context_processors.i18n",
   "django.core.context_processors.media",
   "django.core.context_processors.static",
    "django.core.context_processors.tz",
   "django.contrib.messages.context_processors.messages",
   'django.core.context_processors.request',
   )
   LANGUAGE_CODE = 'en-us'

 TIME_ZONE = 'UTC'

 USE_I18N = True

 USE_L10N = True

 USE_TZ = True  

MEDIA_ROOT = os.path.join(BASE_DIR, 'static/uploaded_image')
 # Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.6/howto/static-files/

STATIC_URL = '/static/'
STATICFILES_DIRS = (os.path.join(BASE_DIR, 'static'),
# Put strings here, like "/home/html/static" or "C:/www/django/static".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.
)

from .email_info import *

EMAIL_USE_TLS = EMAIL_USE_TLS
EMAIL_HOST = EMAIL_HOST
EMAIL_HOST_USER = EMAIL_HOST_USER
EMAIL_HOST_PASSWORD = EMAIL_HOST_PASSWORD
EMAIL_PORT = EMAIL_PORT

AUTH_PROFILE_MODULE='student.student'

form.py是

from django.forms import ModelForm
from student.models import student
class student_form(ModelForm):
    class Meta:
        model=student
        fields = ['branch', 'image', 'dateofbirth']

model.py是

class student(models.Model):
   u=models.OneToOneField(User)
   sex=models.CharField(max_length=10)
   branch=models.CharField(max_length=10)
   dateofbirth=models.CharField(max_length=20)
   image=models.CharField(max_length=20)
   course_taken=models.ManyToManyField(Course,null=True, blank=True)
   def __str__(self):
      return self.u.username

User.profile=property(lambda u: student.objects.get_or_create(u=u)[0])

url.py是

from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Examples:
    (r'^quizes/',include('quiz.urls')),
    url(r'^$', 'student.views.home', name='home'),
    url(r'^login$', 'student.views.login_view', name='login'),
    url(r'^course$', 'student.views.courseView', name='courses'),
    url(r'^admin/', include(admin.site.urls)),
    url(r'^faculty$', 'student.views.faculty', name='faculty'),
    url(r'^about$', 'smvdu_portal.views.about', name='about'),
    url(r'^logout$', 'student.views.logout_view', name='logout'),
    url(r'^reset/$', 'student.views.reset', name='reset'),
    # Map the 'app.hello.reset_confirm' view that wraps around built-in password
   # reset confirmation view, to the password reset confirmation links.
   url(r'^reset/(?P<uidb64>[0-9A-Za-z_\-]+)/(?P<token>[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})/$',
    'student.views.reset_confirm', name='password_reset_confirm'),

# Map the 'app.hello.success' view to the success message page.
url(r'^success/$', 'student.views.success', name='success'),
url(r'^success2/$', 'student.views.success2', name='success2'),
url(r'^edit/$','student.views.edit',name='edit')

)

我收到错误

IntegrityError at /edit/
NOT NULL constraint failed: student_student.u_id

0 个答案:

没有答案
相关问题