Django用户配置文件 [英] Django user profile

查看:117
本文介绍了Django用户配置文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在向用户个人资料添加其他字段(如位置,性别,雇主等)时,应该向 django.contrib.auth.models.User 并保存在那里?或者我应该创建一个新的表来保存用户个人资料?



此外,当用户上传个人资料图片时,应该将其保存在同一张表中吗? (注意,这不是一个生产服务器,我只是在我的本地runserver来做这个事情)。谢谢

解决方案

您必须为用户个人资料制作模型:



pre $ class UserProfile(models.Model):
user = models.ForeignKey(User,unique = True)
location = models.CharField(max_length = 140)
gender = models.CharField(max_length = 140)
雇主= models.ForeignKey(雇主)
profile_picture = models.ImageField(upload_to ='thumbpath',blank = True)

def __unicode __(self):
return u'Profile of user:%s'%self.user.username

然后在 settings.py 中配置:

  AUTH_PROFILE_MODULE ='accounts.UserProfile'


When adding additional fields to a user profile, such as location, gender, employer, etc., should I be adding additional columns to django.contrib.auth.models.User and saving it there? Or should I be creating a new table to save user profile information?

Also, when a user uploads a profile picture, should I be saving this in the same table? (Note this is not a production server, I'm just doing this on my local runserver to figure things out). Thank you

解决方案

You have to make a model for the user profile:

class UserProfile(models.Model):  
    user = models.ForeignKey(User, unique=True)
    location = models.CharField(max_length=140)  
    gender = models.CharField(max_length=140)  
    employer = models.ForeignKey(Employer)
    profile_picture = models.ImageField(upload_to='thumbpath', blank=True)

    def __unicode__(self):
        return u'Profile of user: %s' % self.user.username

Then configure in settings.py:

AUTH_PROFILE_MODULE = 'accounts.UserProfile'

这篇关于Django用户配置文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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