[英]django-registration app and Django 1.5 custom user model
我使用django-registration app和Django 1.5。 如何創建(django中的新)自定義用戶模型並在注冊期間保存此數據(請注意我使用的是django-registration):
class CustomProfile(models.Model):
user = models.ForeignKey(User)
name = models.CharField(max_length=255)
bank = models.CharField(max_length=255)
address = models.CharField(max_length=255)
?
此鏈接很好地解釋了該過程並與django-registration 1.0一起使用
除了上面的代碼,這里還有一些額外的指針。
要更新名字,請在models.py中更改此名稱
def user_registered_callback(sender, user, request, **kwargs):
profile = ExUserProfile(user = user)
profile.is_human = bool(request.POST["is_human"])
user.first_name = request.POST["firstname"]
user.save()
profile.save()
user_registered.connect(user_registered_callback)
並在forms.py文件中
class ExRegistrationForm(RegistrationForm):
is_human = forms.BooleanField(label = "Are you human?:")
firstname = forms.CharField(max_length=30)
lastname = forms.CharField(max_length=30)
最后看到表單上的更改創建了一個合適的模板。 通過在應用程序中創建名為admin.py的文件,可以在管理員中看到該配置文件,並編寫以下代碼
from django.contrib import admin
from django.contrib.auth.models import User
from django.contrib.auth.admin import UserAdmin
from prof.models import ExUserProfile
admin.site.unregister(User)
class UserProfileInline(admin.StackedInline):
model = ExUserProfile
class UserProfileAdmin(UserAdmin):
inlines = [ UserProfileInline, ]
admin.site.register(User, UserProfileAdmin)
Django-registration 1.0最近已經發布。 它被完全重寫為使用基於類的視圖和Django 1.0自定義用戶模型支持。 查看文檔 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.