Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/362.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 - Fatal编程技术网

Python Django——上传后硬设置的值

Python Django——上传后硬设置的值,python,django,Python,Django,所以我有这个模型课 class Document(models.Model): docfile = models.FileField(upload_to=_upload_path) user = models.ForeignKey(User) user_id = user.primary_key options = 0 _upload_path = #... 由两个上载功能使用 def list(request): newdoc = None

所以我有这个模型课

class Document(models.Model):
    docfile = models.FileField(upload_to=_upload_path)
    user = models.ForeignKey(User)
    user_id = user.primary_key
    options = 0

    _upload_path = #...
由两个上载功能使用

def list(request):
    newdoc = None
    # Handle file upload
    if request.method == 'POST':
        form = DocumentForm(request.POST, request.FILES)
        if form.is_valid():
            newdoc = Document(docfile = request.FILES['docfile'])
            newdoc.user = request.user
            newdoc.options = 0
            newdoc.save()
            # Redirect to the document list after POST
            return HttpResponseRedirect(reverse('notendur.views.list'))
    else:
        form = DocumentForm() # An empty, unbound form

    # Load documents for the list page
    documents = Document.objects.all()

    # Render list page with the documents and the form
    return render_to_response(# stuff)


因此,
options
属性充当两个上载函数之间的标识符,因此我知道哪个函数上载了哪个文档。我的问题是,无论我做什么,
options
属性似乎都设置为
0

就像您对问题的评论一样,您不应该在模型上设置
options=0
,而应该说
models.PositiveIntegerField(默认值=0)
。另外,在您的模型上指定
user\u id
是不必要的,因为您已经指定了
user
ForeignKey

这可能不会回答您的问题,但不要调用函数列表,因为它是保留名称。您是对的,我最好更改它。
def reikna(request):
    if request.method == 'POST':
        form = DocumentForm(request.POST, request.FILES)
        if form.is_valid():
            new_doc = Document(docfile = request.FILES['docfile'])
            new_doc.user = request.user
            # Marks the file as /options/ file
            new_doc.options = 1
            new_doc.save()

    else:
        form = DocumentForm() # An empty, unbound form

    render_to_response( #stuff )