Python Django如何添加表单以外的模型

Python Django如何添加表单以外的模型,python,django,Python,Django,我正在使用Django2.0制作一个应用程序。但有些事情即使我调查也不知道。 有以下几种模式 class Picture(models.Model): place = models.ForeignKey(Place, on_delete=models.CASCADE) title = models.CharField(max_length=255) image = models.ImageField(upload_to='image/') 使用以下表格 class Pi

我正在使用Django2.0制作一个应用程序。但有些事情即使我调查也不知道。 有以下几种模式

class Picture(models.Model):
    place = models.ForeignKey(Place, on_delete=models.CASCADE)
    title = models.CharField(max_length=255)
    image = models.ImageField(upload_to='image/')
使用以下表格

class PictureForm(forms.ModelForm):
    class Meta:
        model = Picture
        fields = ['place', 'title', 'image']
并在views.py上运行以下代码

obj = Picture()
pic = PictureForm(request.POST, request.FILES, instance=obj)
pic.save()
我可以和平地添加模型。但这样,我每次都应该在表格上选择位置和标题。在我决定位置和标题之前,我只选择表单上的图像。它减少了我的工作量。 由于有几个位置和标题,我不想将它们设置为默认值。 我成功地做了一个表格

class PictureForm(forms.ModelForm):
    class Meta:
        model = Picture
        fields = ['image',]
但我不知道如何在views.py上添加标题和位置

obj = Picture()
pic = PictureForm(request.POST, request.FILES, instance=obj)
pic.save()

我的英语不好。感谢您在文章末尾看到了糟糕的句子,您的问题来自视图代码,您在视图代码中实例化了新模型。 如果要创建新模型,则不需要传递insance参数,只需编辑行即可获得以下结果:

pic = PictureForm(request.POST, request.FILES)
pic.save()

为了能够只设置几个字段,您应该更新模型声明,例如在可选字段参数中使用
null=True、Blank=True、default=None


注:django文档可以用多种语言进行转换,但是如果您没有找到您的文档,请尝试IRC频道。

您的问题来自视图代码,您可以在其中实例化新模型。 如果要创建新模型,则不需要传递insance参数,只需编辑行即可获得以下结果:

pic = PictureForm(request.POST, request.FILES)
pic.save()

为了能够只设置几个字段,您应该更新模型声明,例如在可选字段参数中使用
null=True、Blank=True、default=None


注:django文档可以用多种语言进行转换,但是如果您没有找到您的文档,请尝试IRC频道。

如果我清楚地理解您的问题,
您已经预先指定了位置和标题,希望用户只输入图像
表单中只应保留图像,但您希望在视图中输入标题和位置。
如果是这样,请执行以下操作:

您的查看功能:

def some_view(request):
    if request.method == "POST":

        T,P = get_title_and_place() 
        # This fuction will return a tuple with the title and the place that you need.
        # Or Alternatively, you can pass the title and place as argument to the view

        obj = Picture(title=T,place=P)
        #Initialise object with title and place received from function or arguments

        pic_form = PictureForm(request.POST, request.FILES)
        #The form contains only the image field as in your last block of code

        if pic_form.is_valid():
            obj.image = request.FILES['image']
            obj.save()
            #Save the object
            return redirect('some_url')

        else:          
           return render('xyz.html',{"form":pic_form})
           #Incoming image is invalid. Throw back the invalid form to the user

    else:
        #Incoming method is not POST, give user the form to fill the image
        pic_form = PictureForm()
        return render('xyz.html',{"form:pic_form})
从xyz.html提交表单时,应重定向回此视图
这就是为什么我做了一个单独的函数来获取标题和位置,这样用户就不会通过url知道它了



然而,使用这种方法时要小心。您必须注意验证标题和位置。否则,如果我清楚地理解您的问题,obj.save()将抛出错误,
您已经预先指定了位置和标题,希望用户只输入图像
表单中只应保留图像,但您希望在视图中输入标题和位置。
如果是这样,请执行以下操作:

您的查看功能:

def some_view(request):
    if request.method == "POST":

        T,P = get_title_and_place() 
        # This fuction will return a tuple with the title and the place that you need.
        # Or Alternatively, you can pass the title and place as argument to the view

        obj = Picture(title=T,place=P)
        #Initialise object with title and place received from function or arguments

        pic_form = PictureForm(request.POST, request.FILES)
        #The form contains only the image field as in your last block of code

        if pic_form.is_valid():
            obj.image = request.FILES['image']
            obj.save()
            #Save the object
            return redirect('some_url')

        else:          
           return render('xyz.html',{"form":pic_form})
           #Incoming image is invalid. Throw back the invalid form to the user

    else:
        #Incoming method is not POST, give user the form to fill the image
        pic_form = PictureForm()
        return render('xyz.html',{"form:pic_form})
从xyz.html提交表单时,应重定向回此视图
这就是为什么我做了一个单独的函数来获取标题和位置,这样用户就不会通过url知道它了


然而,使用这种方法时要小心。您必须注意验证标题和位置。否则,obj.save()将抛出错误