Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python django-多对多字段上的文件上载_Python_Django_Django Models_Django Forms_Django Views - Fatal编程技术网

Python django-多对多字段上的文件上载

Python django-多对多字段上的文件上载,python,django,django-models,django-forms,django-views,Python,Django,Django Models,Django Forms,Django Views,我在模型方面有一个多对多的领域- class A(models.Model): file = models.ManyToManyField(B, blank=True) 在模型中引用另一个类 class B(models.Model): filename = models.FileField(upload_to='files/') user = models.ForeignKey(User) forms.py class AForm(forms.ModelForm):

我在模型方面有一个多对多的领域-

class A(models.Model):
    file = models.ManyToManyField(B, blank=True)
在模型中引用另一个类

class B(models.Model):
    filename = models.FileField(upload_to='files/')
    user = models.ForeignKey(User)
forms.py

class AForm(forms.ModelForm):
    file = forms.FileField(label='Select a file to upload', widget=forms.ClearableFileInput(attrs={'multiple': True}), required=False)
    class Meta:
        model = A
        fields = '__all__'
如何在这里上传文件?我在这里得到了.py建议的基本视图-不起作用

编辑: views.py

阿贾克斯-


我是这样想的:

def your_view(request, a_id):
    a = A.objects.get(id=int(a_id))

    if request.method == "POST" :
        aform = AForm(request.POST, instance=a)

        if aform.is_valid():
            files = request.FILES.getlist('file') #'file' is the name of the form field.

            for f in files:
                a.file.create(filename=f, user=request.user)
                # Here you create a "b" model directly from "a" model

    return HttpResponseRedirect(...)
编辑: 如果之前未创建模型,则不能在表单中使用实例。您正在执行
a=a()
,它正在调用
\uuuu init\uuuu
方法,但没有创建它。另外,我不得不说,这有点奇怪,因为你需要在A之前创建B,这样你就可以在文件ManyToManyField中看到B模型

def your_view(request):

    if request.method == "POST" :
        aform = AForm(request.POST, request.FILES)

        if aform.is_valid():
            a = aform.save() # Here you have the a model already created
            files = request.FILES.getlist('file') #'file' is the name of the form field.

            for f in files:
                a.file.create(filename=f, user=request.user)
                # Here you create a "b" model directly from "a" model

    return HttpResponseRedirect(...)

感谢DavidRguez的回复,它给了我一个错误“未选择任何文件”。尽管如此,我还是在您的建议中添加了一个form.save()语句。有什么建议吗?我需要看一下你的模板,以便全面了解你的工作。请参阅我的编辑以了解更多信息。更新的视图和模板-现在,表单-无论我是否输入,似乎都是无效的,因为其呈现的表单响应非常清晰。请建议我也理解你做a=a()的观点,但如何创建新模型?嗨,大卫,现在发生的是formdata是空的。我已经检查了chrome developer tools中的网络选项卡,请求负载部分为空,我不确定为什么会发生这种情况:(
def your_view(request, a_id):
    a = A.objects.get(id=int(a_id))

    if request.method == "POST" :
        aform = AForm(request.POST, instance=a)

        if aform.is_valid():
            files = request.FILES.getlist('file') #'file' is the name of the form field.

            for f in files:
                a.file.create(filename=f, user=request.user)
                # Here you create a "b" model directly from "a" model

    return HttpResponseRedirect(...)
def your_view(request):

    if request.method == "POST" :
        aform = AForm(request.POST, request.FILES)

        if aform.is_valid():
            a = aform.save() # Here you have the a model already created
            files = request.FILES.getlist('file') #'file' is the name of the form field.

            for f in files:
                a.file.create(filename=f, user=request.user)
                # Here you create a "b" model directly from "a" model

    return HttpResponseRedirect(...)