2014-04-06 51 views
1

我不想更改默認身份驗證模型。Django - 附加(不覆蓋)自定義模型方法給用戶

我只是想添加其他方法對用戶是這樣的:

def update_first_name(self, first_name): 
    self.first_name = first_name 
    self.save() 

這是一個壞榜樣,但我想這樣的功能。這應該只是額外的,不應該放棄任何其他可用的用戶方法。

+0

我會去爲用戶配置和寫有我想要的任何方式。 https://docs.djangoproject.com/en/1.4/topics/auth/#storing-additional-information-about-users – doniyor

回答

2

User創建一個proxy model並在其中添加您的方法。

class ProxyUser(User): 
    def do_something(self, ...): 
    ... 

    class Meta: 
    proxy = True 
+0

然後,我應該如何獲得用戶對象,User.objects.get或ProxyUser.objects.get ??我想使用User.objects.get,因爲可能有一個項目,其中User.objetcs.get已被使用,我不想更改全部。 – user2349115

1

要增加新功能到User類是通過定義代理模型。
這意味着creating a proxy for the original model.

class UserExtend(User): 
     class Meta: 
      proxy= True 

     def update_first_name(self, first_name): 
      self.first_name = first_name 
      self.save()