简体   繁体   English

我在哪里存储用户个人信息

[英]Where do I store user personal information

I am creating an application in which a user registers with a username, email, and password.我正在创建一个用户使用用户名、电子邮件和密码注册的应用程序。 This data is stored in one table.此数据存储在一个表中。 But then the user enters profile information such as photo, bio, activity and such.但随后用户输入个人资料信息,例如照片、简历、活动等。 Do I store this data in the same table as the username, email, and password?我是否将此数据与用户名、电子邮件和密码存储在同一个表中? Or do I create another table in which I link those in some way?或者我是否创建另一个表以某种方式链接它们?

you can create a different table UserProfile with OneToOne relation.您可以使用 OneToOne 关系创建不同的表 UserProfile。

class UserProfile(models.Model):  
    user = models.OneToOne(User, unique=True)
    location = models.CharField(max_length=140)  
    bio = models.CharField(max_length=140)  
    profile_picture = models.ImageField(upload_to='profile_pics', blank=True)

Then in settings.py:然后在settings.py中:

 AUTH_USER_MODEL = 'accounts.User'

You want to extend User model to have other information.您想扩展 User 模型以获取其他信息。 The recomended approach for this is to create another model like Profile and setup OneToOne relation to the User.推荐的方法是创建另一个模型,如 Profile 并设置与用户的 OneToOne 关系。

You can create a new table that have the information you want to store but the most important thing is to connect the new table with the userid from user id您可以创建一个包含要存储的信息的新表,但最重要的是将新表与用户 ID 中的用户 ID 连接起来

CREATE TABLE USER_INFORMATIONS(
Information_ID SERIAL PRIMARY KEY,
USER_ID (int or varchar acoording to the user table),
…
FOREIGN KEY (USER_ID) REFERENCES Users(Id)
);

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

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