2014-09-27 55 views
0

我试图通过降低所有用户的昵称来清理数据。这里是代码:UserProfile在尝试清理数据时没有用户

class UserProfile(models.Model): 
    # This line is required. Links UserProfile to a User model instance. 
    user = models.OneToOneField(User) 

    # The additional attributes we wish to include. 
    website = models.URLField(blank=True) 

    def __unicode__(self): 
     return self.user.username 

    def clean(self): 
     self.user.username = self.user.username.lower() 

但是我得到`UserProfile没有用户。

我很喜欢Django,但我是否正确地理解,只要我们保存我们的模型 - 使用我编写的clean()方法?那是对的吗?那么我的错误在哪里?

编辑:

def register(request): 
    # Like before, get the request's context. 
    context = RequestContext(request) 

    # A boolean value for telling the template whether the registration was successful. 
    # Set to False initially. Code changes value to True when registration succeeds. 
    registered = False 

    # If it's a HTTP POST, we're interested in processing form data. 
    if request.method == 'POST': 
     # Attempt to grab information from the raw form information. 
     # Note that we make use of both UserForm and UserProfileForm. 
     user_form = UserForm(data=request.POST) 
     profile_form = UserProfileForm(data=request.POST) 

     # If the two forms are valid... 
     if user_form.is_valid() and profile_form.is_valid(): 
      # Save the user's form data to the database. 
      user = user_form.save() 


      # Now we hash the password with the set_password method. 
      # Once hashed, we can update the user object. 
      user.set_password(user.password) 
      user.save() 

      # Now sort out the UserProfile instance. 
      # Since we need to set the user attribute ourselves, we set commit=False. 
      # This delays saving the model until we're ready to avoid integrity problems. 
      profile = profile_form.save(commit=False) 
      profile.user = user 

      # Now we save the UserProfile model instance. 
      profile.save() 

      # Update our variable to tell the template registration was successful. 
      registered = True 

     # Invalid form or forms - mistakes or something else? 
     # Print problems to the terminal. 
     # They'll also be shown to the user. 
     else: 
      print user_form.errors, profile_form.errors 

    # Not a HTTP POST, so we render our form using two ModelForm instances. 
    # These forms will be blank, ready for user input. 
    else: 
     user_form = UserForm() 
     profile_form = UserProfileForm() 

    # Render the template depending on the context. 
    return render_to_response(
      'registration.html', 
      {'user_form': user_form, 'profile_form': profile_form, 'registered': registered}, 
      context) 
+0

请准确显示您的错误。 – 2014-09-27 10:41:12

+0

我正在尝试注册一个新用户。我添加了注册() – 2014-09-27 10:46:56

+0

错误追溯将是非常有益的,请张贴您的表单代码 – 2014-09-27 11:27:16

回答

0

模型清洁方法不被保存过程调用,而是由表格,当你调用is_valid()那里。你可以通过观察回溯来看到自己 - 它总是有用的信息。

在is_valid被调用的地方,当然,该配置文件没有用户,因此错误。

我必须说,虽然这很少有任何意义无论如何。为什么你把这些代码放在Profile模型中,而不是User模型中?更好的是,它应该在的形式,在clean_username方法,其中它应该返回下壳值。