gpt4 book ai didi

python - 如何将django模型保存在数据库中以供用户自定义 View ?

转载 作者:太空宇宙 更新时间:2023-11-03 17:24:00 25 4
gpt4 key购买 nike

假设我有三个模型,在我看来,我正在显示这些模型的所有项目,我想授予我的用户权限来设置在第一个、第二个和第三个 View 中显示哪个模型的对象。

实现这个的最佳方法是什么?

最佳答案

Django 中的最佳实践是按如下方式定义用户配置文件对象:

class UserProfile(models.Model):
user = models.OneToOneField(User, related_name="profile")
# add whatever other configurations & preferences you allow the user to set here
feature_x_priority = models.IntegerField(default=0)
feature_y_priority = models.IntegerField(default=1)
feature_z_priority = models.IntegerField(default=2)

然后,您可以在代码中的任何位置使用user.profile.feature_x_priority

不要忘记为每个用户(新用户和现有用户)创建一个配置文件,否则您将在使用之前检查 user.profile 是否存在。

关于python - 如何将django模型保存在数据库中以供用户自定义 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32816966/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com