2015-09-28 41 views

回答

1

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

0

不清楚「我的用戶」是什麼意思。

這只是管理員用戶誰設置此配置globaly爲該網站。

還是你的意思是evey網站的用戶有他/她自己的喜好?

如果後一種情況下,則創建一個名爲Preferences的新模型,該模型與用戶模型具有one to one關係。

然後在您的查詢中,您應根據首選項值創建三個單獨的查詢。

相關問題