用户是必需的错误消息

时间:2013-01-25 03:45:24

标签: python django

print profile_form.errors

节目:

<ul class="errorlist"><li>user<ul class="errorlist"><li>This field is required.</li></ul></li></ul>

代码:

def update(request):
    user    = get_object_or_404(User, pk=request.user.id)
    profile = get_object_or_404(UserProfile, user=user)

    if request.method == 'POST':
        user_form    = RegistrationForm(request.POST, instance=user)
        profile_form = ProfileForm(request.POST, request.FILES, instance=profile)

---

class ProfileForm(forms.ModelForm):
    address   = forms.CharField(required=False, max_length=250, widget=forms.Textarea(attrs={'rows': 2}))

    class Meta:
        model = UserProfile

---

class UserProfile(models.Model):
    ...
    country             = models.ForeignKey(Country, default=1)
    city                = models.ForeignKey(City, default=1)
    user                = models.OneToOneField(User)

我如何绕过它,因为配置文件已经分配给用户。

1 个答案:

答案 0 :(得分:2)

您的表单可能无法呈现用户字段,ModelForm正在等待一个。

class ProfileForm(forms.ModelForm):
    address   = forms.CharField(required=False, max_length=250, widget=forms.Textarea(attrs={'rows': 2}))

    class Meta:
        model = UserProfile
        exclude = ('user', )  # <-- exclude user from your form