向django中的用户添加自定义字段 [英] Adding custom fields to users in django

查看:109
本文介绍了向django中的用户添加自定义字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Django提供的create_user()函数来创建我的用户。此外,我想存储有关用户的其他信息。所以我尝试按照



http://docs.djangoproject.com/en/dev/topics/auth/#storing-additional-information-about-users



但我不能让它为我工作。有没有一个循序渐进的指导,我可以遵循这一点,让我为我工作?



此外,一旦我添加了这些自定义字段,我显然需要从中添加/编辑/删除数据。我似乎找不到有关如何做到这一点的任何指示。

解决方案

我不知道一步一步我肯定一个扎实的谷歌会产生一些东西)。



1)创建一个 UserProfile 模型来保存额外的信息并放置它在你的 models.py 中。它可能看起来像这样:

  class UserProfile(models.Model):
#由auth模型请求
user = models.ForeignKey(User,unique = True)
middle_name = models.CharField(max_length = 30,null = True,blank = True)
pre>

2)通过添加此行(使用适当的名称)告诉您的新课程 settings.py

  AUTH_PROFILE_MODULE =myapp.UserProfile

3)添加一个信号侦听器以在添加新用户时创建一个空白的 UserProfile 记录。您可以在这里找到一个很棒的片段。



4)处理新用户记录时,您还可以填充 UserProfile 记录。这是我如何做插入(注意 get_profile ):

  if(form.is_valid()):
cd = form .cleaned_data
user = User.objects.create_user(cd [UserName],cd [Email],cd [Password])
user.first_name = cd [FirstName]
user.last_name = cd [LastName]
user.save()
#Save userinfo record
uinfo = user.get_profile()
uinfo.middle_name = cd [MiddleName]
uinfo.save()

这就是它所有。这不是全面的,但应该指向正确的方向。



更新:请注意, AUTH_PROFILE_MODULE 自v1.5以来已弃用: https ://docs.djangoproject.com/en/1.5/ref/settings/#auth-profile-module


I am using the create_user() function that Django provides to create my users. Also I want to store additional information about the users. So I tried following the instructions given at

http://docs.djangoproject.com/en/dev/topics/auth/#storing-additional-information-about-users

but I cannot get it to work for me. Is there a step-by-step guide that I can follow to get this to work for me?

Also, once I have added these custom fields, I would obviously need to add / edit / delete data from them. I cannot seem to find any instructions on how to do this.

解决方案

I am not aware of a step by step(though I am sure a solid google would produce something). But here is a quick go at it.

1) Create a UserProfile model to hold the extra information and put it in your models.py. It could look something like this:

class UserProfile(models.Model):
    #required by the auth model
    user = models.ForeignKey(User, unique=True)
    middle_name = models.CharField(max_length=30, null=True, blank=True)

2) Tell your settings.py about the new class by adding this line (with the appropriate name):

AUTH_PROFILE_MODULE = "myapp.UserProfile"

3) Add a signal listener to create a blank UserProfile record when a new user is added. You can find a great snippet with directions here.

4) When processing the new user record you can populate the UserProfile record as well. Here is how I do the insert (notice the get_profile):

if (form.is_valid()):
    cd = form.cleaned_data
    user = User.objects.create_user(cd["UserName"], cd["Email"], cd["Password"])
    user.first_name = cd["FirstName"]
    user.last_name = cd["LastName"]
    user.save()
    #Save userinfo record
    uinfo = user.get_profile()
    uinfo.middle_name = cd["MiddleName"]
    uinfo.save()

That is all there is to it. This is not comprehensive, but should point you in the right direction.

Update: Please note that AUTH_PROFILE_MODULE is deprecated since v1.5: https://docs.djangoproject.com/en/1.5/ref/settings/#auth-profile-module

这篇关于向django中的用户添加自定义字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆