简体   繁体   English

django-registration app和Django 1.5自定义用户模型

[英]django-registration app and Django 1.5 custom user model

I use django-registration app and Django 1.5. 我使用django-registration app和Django 1.5。 How to create (new in django) custom user model and save also this data during registration (Please note that I am using django-registration): 如何创建(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's main fork is not compatible with django 1.5 for now. django-registration的主叉现在与django 1.5不兼容。

Check this pull request . 检查此拉取请求

You have three options: 你有三个选择:

  • Patch django-registration's code. 补丁django-registration的代码。 You can get the changes needed from the pull request. 您可以从pull请求中获取所需的更改。
  • Use an unofficial fork that is already patched. 使用已修补的非官方fork。 This one for example. 例如这一个
  • Wait for an update on the main fork... 等待主叉更新......

This link explains the process well and works with django-registration 1.0 链接很好地解释了该过程并与django-registration 1.0一起使用

here are a few extra pointers in addition to the above code. 除了上面的代码,这里还有一些额外的指针。

To update the first name change this in the models.py 要更新名字,请在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)

and in the forms.py file 并在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)

finally to see the changes on the form create an appropriate template. 最后看到表单上的更改创建了一个合适的模板。 The profile can be seen in the admin by creating a file called admin.py in your app and write the following code 通过在应用程序中创建名为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 has been released recently. Django-registration 1.0最近已经发布。 It was completely rewritten to use class based views and with Django 1.0 custom user model support. 它被完全重写为使用基于类的视图和Django 1.0自定义用户模型支持。 Check out the docs . 查看文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM