2014-02-12 59 views
1

我想從當前用戶的信息在我的forms.pyDjango。 get_profile()不工作

我的模型:

class Users(models.Model): 
    first_name = models.CharField('First name', max_length=50) 
     .... 
    user = models.OneToOneField(User, editable=False) 

我的觀點:

@login_required 
def editAccount(request): 
    if request.method == 'POST': 
     form = EditAccountForm(request.user, request.POST) 
     if form.is_valid(): 
      form.save() 

我的表格:

class EditAccountForm(forms.ModelForm): 
    class Meta: 
     model = Users 

    def __init__(self, user, *args, **kwargs): 

     self.user = user.get_profile() 

我把我的settings.py AUTH_PROFILE_MODULE = 「user.Users」,我有THI s錯誤:棄用警告:使用AUTH_PROFILE_MODULE來定義用戶配置文件已被棄用。 self.user = user.get_profile() 我是新的django我沒有看到我的錯誤。 非常感謝

回答

1

的AUTH_PROFILE_MODULE設置和django.contrib.auth.models.User.get_profile()在Django 1.5中貶值見AUTH_PROFILE_MODULE

爲您的代碼試試這個:

models.y

class Users(models.Model): 
    user = OneToOneField(User, related_name="profile") 

forms.py

class EditAccountForm(forms.ModelForm): 

    class Meta: 
     model = Users 

    def __init__(self, user, *args, **kwargs): 
     self.user = user.profile 

Here's the related question with answers

-1

您可以做的另一件事是簡單地調用創建用戶的模型和請求信息的 。 IE

您創建的用戶配置文件。

class User(models.Model): 
    username = models.OneToOneField(User, editable=False) 
    first_name = models.CharField("Last name", max_length=50) 
    last_name = models.CharField("First Name", max_length=50) 
    email = models.EmailField(blank=True) 

    def __str__(self): 
     return "%s" % self.username 

然後在您的視圖,你可以做類似

class UserProfileView(View): 
    def get(self, request): 
     self.user = request.user 

     #from here you can call your user model requesting for the user information. 
     self.userdetails = Users.objects.get(username=self.user) 
     if(self.userdetails): 
      #user exists 
      #you can pass this to the templates and perform actions on using loops 
      for details in self.userdetails: 
       print details.username 
       print details.first_name 
       print details.last_name 
       #.....