1

我正在开发一个应用程序,宠物用户可以在其中创建个人资料并上传他们的宠物照片。

当我访问我似乎无法理解的个人资料页面时出现此错误。我最近创建了一个与此类似的应用程序,但曾经见过我将我的模型与用户相关联。我得到了错误。我感谢大家的所有帮助,我感谢大家。

我试图让用户在个人资料页面上传自己的照片。

 pet_person.user_id may not be NULL

 Traceback Switch to copy-and-paste view
 C:\o\mysite\pet\views.py in Profile 
        form.save()

我认为这个错误与我的模型和我在 views.py 中的 Profile 函数有关

我的模型.py

  from django.db import models
  from django.db.models.signals import post_save
  from django.contrib.auth.models import User

class Person(models.Model):
    user = models.OneToOneField(User)
    name = models.CharField(max_length=100)
    image = models.FileField(upload_to="images/",blank=True,null=True)


    def __unicode__(self):
        return self.name

class Pet(models.Model):
    Person = models.ForeignKey(Person)
    description = models.CharField(max_length=100)
    image = models.FileField(upload_to="images/",blank=True,null=True)

    def __unicode__(self):
        return self.description

我的部分观点.py

def Profile(request):
    form = PictureForm(request.POST ,request.FILES or None)
    if form.is_valid():
        form.save()
    return render_to_response('profile.html',{'form':PictureForm()},context_instance = RequestContext(request)
)    

我的表格.py

 from django import forms
 from django.contrib.auth.models import User
 from django.forms import ModelForm
 from pet.models import *

 class UserRegistration(ModelForm):
    username = forms.CharField(widget = forms.Textarea)
    name = forms.CharField(widget = forms.Textarea)
    email = forms.EmailField(widget = forms.Textarea)
    password = forms.CharField(widget = forms.PasswordInput(render_value=False))
    password1 = forms.CharField(label =(u'Verify Password'), widget = forms.PasswordInput(render_value=False))

    class Meta:
        model = Person
        exclude = ('user',)
        fields = ('username','email','name','password','password1')

    def clean_username(self):
        username = self.cleaned_data['username']
        try:
            User.objects.get(username=username)
        except User.DoesNotExist:
            return username
        raise forms.ValidationError("That user is already taken , please select another ")

    def clean(self):
        if self.cleaned_data['password'] != self.cleaned_data['password1']:
            raise forms.ValidationError("The password does not match ")
        return self.cleaned_data

 class LoginForm(forms.Form):
    username = forms.CharField(widget = forms.Textarea)
    password = forms.CharField(widget = forms.PasswordInput(render_value=False))
    model = Person
    fields = ('username','password')


 class PictureForm(forms.ModelForm):

    class Meta:
        model = Person
        fields = ('image',)

@掌握!

def Profile(request):
    form = PictureForm(request.POST ,request.FILES or None)
    if form.is_valid():
        event = form.save(commit=False)
        event.user=request.user
    event.save()
    return render_to_response('profile.html',{'form':PictureForm()},context_instance = RequestContext(request))    
4

3 回答 3

1
class PictureForm(forms.ModelForm):
    class Meta:
        model = Person
        fields = ('image',)

def Profile(request):
    person = Person(user=request.user)
    if request.method == 'POST':
        form = PictureForm(request.POST ,request.FILES or None, instance=person)
        if form.is_valid():
            form.save() 
    return render_to_response('profile.html',{'form':PictureForm()},context_instance = RequestContext(request)
于 2013-03-08T13:43:09.380 回答
1

在下面的模型中

class Person(models.Model):
    user = models.OneToOneField(User)
    name = models.CharField(max_length=100)
    image = models.FileField(upload_to="images/",blank=True,null=True)

您有一个名为user That 的字段,在您的表单中的任何地方都没有提到。

并且根据您的模型Person 字段不能为 null 。

克服这个问题

保存您的表格

`obj =  form.save(`commit = False`)`
 obj.user =Person(user=request.user) # If user is logged in 
 obj.save()
于 2013-03-08T13:06:51.843 回答
0

我认为您的问题在于PictureForm您只要求图像字段。但是,这种形式实际上是针对Person模型的。您Person需要用户和名称以及图像(实际上,图像是唯一的可选部分)。鉴于这些不是来自您的表格,您需要自己提供这些。请参阅https://docs.djangoproject.com/en/dev/topics/forms/modelforms/#using-a-subset-of-fields-on-the-form

于 2013-03-08T13:01:55.753 回答