这里不是说的是 profile,关于 profile 的看官方
网上有继承 User 的那个更不可用
django 写的太死,扩展下还是很麻烦
还有,下面的这个会在 django 库的 contrib\auth 目录下创建 migrations 目录,如果你觉得这个影响你别的项目的话,那就在这个项目目录下新建 django 目录,把 django 库下的文件全部复制过来,目录结构如 项目目录/django/bin 这样
------------------------------------------------------
后记:
south 的 auth migrations 目录可以定义的,方法来自于 django-primate 这个app
#注意,这里你要在项目目录下建个 users 目录
SOUTH_MIGRATION_MODULES = {
'auth': 'users.migrations',
}
还有,更好的扩展 User model 使用 django-primate 这个库要好很多,参见 https://github.com/aino/django-primate
------------------------------------------------------
开工
F:\hyp\test>django-admin.py startproject my_django
F:\hyp\test>cd my_django
F:\hyp\test\my_django>manage.py startapp my_app
编辑 settings.py,INSTALLED_APPS 加入 my_app 和 south ,如下
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
# Uncomment the next line to enable the admin:
# 'django.contrib.admin',
# Uncomment the next line to enable admin documentation:
# 'django.contrib.admindocs',
'my_app',
'south'
)
DATABASES 配置好引擎并建好数据库,如这里我们配置成sqlite3
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
'NAME': 'test.db', # Or path to database file if using sqlite3.
'USER': '', # Not used with sqlite3.
'PASSWORD': '', # Not used with sqlite3.
'HOST': '', # Set to empty string for localhost. Not used with sqlite3.
'PORT': '', # Set to empty string for default. Not used with sqlite3.
}
}
my_app/models.py 中加入,添加一个字段,添加一个实例方法,一个类方法
关于添加字段参见 http://djangosnippets.org/snippets/2132/
from django.db import models
from django.contrib.auth.models import User
some_field = models.CharField(max_length=32)
some_field.contribute_to_class(User, 'some_field')
def before_save(self):
self.username = 'test'
User.before_save = before_save
@classmethod
def test(cls):
print cls.__name__
User.test = test
创建 auth 的 migration,这样在 syncdb 的时候就会跳过 auth 这个app
F:\hyp\test\my_django>manage.py schemamigration auth --initial
Creating migrations directory at 'F:\hyp\language\Python27\lib\site-packages\django\contrib\auth\migrations'...
Creating __init__.py in 'F:\hyp\language\Python27\lib\site-packages\django\contrib\auth\migrations'...
+ Added model auth.Permission
+ Added unique constraint for ['content_type', 'codename'] on auth.Permission
+ Added model auth.Group
+ Added M2M table for permissions on auth.Group
+ Added model auth.User
+ Added M2M table for groups on auth.User
+ Added M2M table for user_permissions on auth.User
+ Added model auth.Message
Created 0001_initial.py. You can now apply this migration with: ./manage.py migrate auth
创建 my_app 的 migration
F:\hyp\test\my_django>manage.py schemamigration my_app --initial
Creating migrations directory at 'F:\hyp\test\my_django\my_app\migrations'...
Creating __init__.py in 'F:\hyp\test\my_django\my_app\migrations'...
Created 0001_initial.py. You can now apply this migration with: ./manage.py migrate my_app
执行 manage.py syncdb --all 创建表
F:\hyp\test\my_django>manage.py syncdb --all
Syncing...
Creating tables ...
Creating table auth_permission
Creating table auth_group_permissions
Creating table auth_group
Creating table auth_user_user_permissions
Creating table auth_user_groups
Creating table auth_user
Creating table auth_message
Creating table django_content_type
Creating table django_session
Creating table django_site
Creating table south_migrationhistory
You just installed Django's auth system, which means you don't have any superuse
rs defined.
Would you like to create one now? (yes/no): no
Installing custom SQL ...
Installing indexes ...
No fixtures found.
Synced:
> django.contrib.auth
> django.contrib.contenttypes
> django.contrib.sessions
> django.contrib.sites
> django.contrib.messages
> django.contrib.staticfiles
> my_app
> south
Not synced (use migrations):
-
(use ./manage.py migrate to migrate these)
好了,基本上搞定了,下面我们来测试一下
F:\hyp\test\my_django>manage.py shell
Python 2.7.1 (r271:86832, Nov 27 2010, 18:30:46) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from my_app.models import *
>>> u = User()
>>> u.username
''
>>> u.before_save()
>>> u.username
'test'
>>> u.some_field
''
>>> User.test()
User
Last modified by vkill on2011/05/27 15:14
网上有继承 User 的那个更不可用
django 写的太死,扩展下还是很麻烦
还有,下面的这个会在 django 库的 contrib\auth 目录下创建 migrations 目录,如果你觉得这个影响你别的项目的话,那就在这个项目目录下新建 django 目录,把 django 库下的文件全部复制过来,目录结构如 项目目录/django/bin 这样
------------------------------------------------------
后记:
south 的 auth migrations 目录可以定义的,方法来自于 django-primate 这个app
#注意,这里你要在项目目录下建个 users 目录
SOUTH_MIGRATION_MODULES = {
'auth': 'users.migrations',
}
还有,更好的扩展 User model 使用 django-primate 这个库要好很多,参见 https://github.com/aino/django-primate
------------------------------------------------------
开工
F:\hyp\test>django-admin.py startproject my_django
F:\hyp\test>cd my_django
F:\hyp\test\my_django>manage.py startapp my_app
编辑 settings.py,INSTALLED_APPS 加入 my_app 和 south ,如下
Quotation
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
# Uncomment the next line to enable the admin:
# 'django.contrib.admin',
# Uncomment the next line to enable admin documentation:
# 'django.contrib.admindocs',
'my_app',
'south'
)
DATABASES 配置好引擎并建好数据库,如这里我们配置成sqlite3
Quotation
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
'NAME': 'test.db', # Or path to database file if using sqlite3.
'USER': '', # Not used with sqlite3.
'PASSWORD': '', # Not used with sqlite3.
'HOST': '', # Set to empty string for localhost. Not used with sqlite3.
'PORT': '', # Set to empty string for default. Not used with sqlite3.
}
}
my_app/models.py 中加入,添加一个字段,添加一个实例方法,一个类方法
关于添加字段参见 http://djangosnippets.org/snippets/2132/
from django.db import models
from django.contrib.auth.models import User
some_field = models.CharField(max_length=32)
some_field.contribute_to_class(User, 'some_field')
def before_save(self):
self.username = 'test'
User.before_save = before_save
@classmethod
def test(cls):
print cls.__name__
User.test = test
创建 auth 的 migration,这样在 syncdb 的时候就会跳过 auth 这个app
F:\hyp\test\my_django>manage.py schemamigration auth --initial
Creating migrations directory at 'F:\hyp\language\Python27\lib\site-packages\django\contrib\auth\migrations'...
Creating __init__.py in 'F:\hyp\language\Python27\lib\site-packages\django\contrib\auth\migrations'...
+ Added model auth.Permission
+ Added unique constraint for ['content_type', 'codename'] on auth.Permission
+ Added model auth.Group
+ Added M2M table for permissions on auth.Group
+ Added model auth.User
+ Added M2M table for groups on auth.User
+ Added M2M table for user_permissions on auth.User
+ Added model auth.Message
Created 0001_initial.py. You can now apply this migration with: ./manage.py migrate auth
创建 my_app 的 migration
F:\hyp\test\my_django>manage.py schemamigration my_app --initial
Creating migrations directory at 'F:\hyp\test\my_django\my_app\migrations'...
Creating __init__.py in 'F:\hyp\test\my_django\my_app\migrations'...
Created 0001_initial.py. You can now apply this migration with: ./manage.py migrate my_app
执行 manage.py syncdb --all 创建表
F:\hyp\test\my_django>manage.py syncdb --all
Syncing...
Creating tables ...
Creating table auth_permission
Creating table auth_group_permissions
Creating table auth_group
Creating table auth_user_user_permissions
Creating table auth_user_groups
Creating table auth_user
Creating table auth_message
Creating table django_content_type
Creating table django_session
Creating table django_site
Creating table south_migrationhistory
You just installed Django's auth system, which means you don't have any superuse
rs defined.
Would you like to create one now? (yes/no): no
Installing custom SQL ...
Installing indexes ...
No fixtures found.
Synced:
> django.contrib.auth
> django.contrib.contenttypes
> django.contrib.sessions
> django.contrib.sites
> django.contrib.messages
> django.contrib.staticfiles
> my_app
> south
Not synced (use migrations):
-
(use ./manage.py migrate to migrate these)
好了,基本上搞定了,下面我们来测试一下
F:\hyp\test\my_django>manage.py shell
Python 2.7.1 (r271:86832, Nov 27 2010, 18:30:46) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from my_app.models import *
>>> u = User()
>>> u.username
''
>>> u.before_save()
>>> u.username
'test'
>>> u.some_field
''
>>> User.test()
User
Last modified by vkill on2011/05/27 15:14



还需要自己修改admin.py中自己添加的字段
关于admin中password is required field
我提交了补丁,
/usr/local/lib/python2.7/dist-packages/primate/auth/forms.py
class UserChangeForm(auth.forms.UserChangeForm):
def __init__(self, *args, **kwargs):
super(UserChangeForm, self).__init__(*args, **kwargs)
self.fields['password'].widget = PassWidget()
***self.fields['password'].required = False***
class PassWidget(forms.Widget):
def render(self, name, value, attrs=None):
return mark_safe(u'<input type="button" value="%s" onclick="window.location.href=\'password/\'"><input type="hidden" name="password" value="%s" />' % (_('Change Password'), value))
不过这样不好,还是改 required 比较好,之前我没有深入研究下,谢谢哈