Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/10.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
Django将字段另存为空_Django_Database - Fatal编程技术网

Django将字段另存为空

Django将字段另存为空,django,database,Django,Database,我有表单字段,有时我希望保持它们为空。 问题是视图中的save()方法预期会出现字段和抛出错误 我的表格: from django import forms from cProfile import label class DocumentForm(forms.Form): docfile = forms.FileField( label='Select a file' ) time_from = forms.DateTimeField(

我有表单字段,有时我希望保持它们为空。 问题是视图中的save()方法预期会出现字段和抛出错误

我的表格:

from django import forms
from cProfile import label


class DocumentForm(forms.Form):
    docfile = forms.FileField(
        label='Select a file'
    )

    time_from = forms.DateTimeField(
        label = 'select range of time, from'
    )

    time_to = forms.DateTimeField(
        label = 'to:', required = False
    )
from django.db import models
import uuid
from django.utils import timezone
from celery.worker.strategy import default


class Document(models.Model):
    docfile = models.FileField(upload_to='documents/%Y/%m/%d')
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    time_from = models.DateTimeField(default=None, null=True, blank=True)
    time_to = models.DateTimeField(default=None, null=True, blank=True)
def list(request):
    # Handle file upload
    if request.method == 'POST':
        form = DocumentForm(request.POST, request.FILES)
        if form.is_valid():
            newdoc = Document(docfile=request.FILES['docfile'], time_from=request.POST['time_from'], time_to=request.POST['time_to'])
            newdoc.save()
我的型号:

from django import forms
from cProfile import label


class DocumentForm(forms.Form):
    docfile = forms.FileField(
        label='Select a file'
    )

    time_from = forms.DateTimeField(
        label = 'select range of time, from'
    )

    time_to = forms.DateTimeField(
        label = 'to:', required = False
    )
from django.db import models
import uuid
from django.utils import timezone
from celery.worker.strategy import default


class Document(models.Model):
    docfile = models.FileField(upload_to='documents/%Y/%m/%d')
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    time_from = models.DateTimeField(default=None, null=True, blank=True)
    time_to = models.DateTimeField(default=None, null=True, blank=True)
def list(request):
    # Handle file upload
    if request.method == 'POST':
        form = DocumentForm(request.POST, request.FILES)
        if form.is_valid():
            newdoc = Document(docfile=request.FILES['docfile'], time_from=request.POST['time_from'], time_to=request.POST['time_to'])
            newdoc.save()
我的观点:

from django import forms
from cProfile import label


class DocumentForm(forms.Form):
    docfile = forms.FileField(
        label='Select a file'
    )

    time_from = forms.DateTimeField(
        label = 'select range of time, from'
    )

    time_to = forms.DateTimeField(
        label = 'to:', required = False
    )
from django.db import models
import uuid
from django.utils import timezone
from celery.worker.strategy import default


class Document(models.Model):
    docfile = models.FileField(upload_to='documents/%Y/%m/%d')
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    time_from = models.DateTimeField(default=None, null=True, blank=True)
    time_to = models.DateTimeField(default=None, null=True, blank=True)
def list(request):
    # Handle file upload
    if request.method == 'POST':
        form = DocumentForm(request.POST, request.FILES)
        if form.is_valid():
            newdoc = Document(docfile=request.FILES['docfile'], time_from=request.POST['time_from'], time_to=request.POST['time_to'])
            newdoc.save()
提交时,我试图将time_to字段保持为空,但它给了我以下错误:

异常值:[u”“”值的格式无效。它必须为 YYYY-MM-DD HH:MM[:ss[.uuuuu][TZ]格式。“]


我认为原因是newdoc.save()希望time\u的DateTimeField格式为u'2017-05-05 12:02:02',得到一个空字符串或类似的东西:u'

您应该创建一个ModelForm而不是表单,只需调用Form.save(),这样做django将帮助您管理您的属性和值

Django将为您的模型创建一个id,因此您不需要创建此id

id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
你的表格

class DocumentForm(forms.ModelForm):
    class Meta:
        model = Document #This will link your form to your model, and django can do his job
        fields = '__all__'

    docfile = forms.FileField(
        label='Select a file'
    )

    time_from = forms.DateTimeField(
        label = 'select range of time, from'
    )

    time_to = forms.DateTimeField(
        label = 'to:', required = False
    )
你的观点

def list(request):
# Handle file upload
if request.method == 'POST':
    form = DocumentForm(request.POST, request.FILES)
    if form.is_valid():
        form.save()

如果您这样做,您的DocumentForm(request.POST、request.FILES)将创建一个Document实例,它将验证并存储正确的值。

您应该创建一个ModelForm而不是表单,只需调用Form.save(),这样做django将帮助您管理属性和值

Django将为您的模型创建一个id,因此您不需要创建此id

id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
你的表格

class DocumentForm(forms.ModelForm):
    class Meta:
        model = Document #This will link your form to your model, and django can do his job
        fields = '__all__'

    docfile = forms.FileField(
        label='Select a file'
    )

    time_from = forms.DateTimeField(
        label = 'select range of time, from'
    )

    time_to = forms.DateTimeField(
        label = 'to:', required = False
    )
你的观点

def list(request):
# Handle file upload
if request.method == 'POST':
    form = DocumentForm(request.POST, request.FILES)
    if form.is_valid():
        form.save()

如果您这样做,您的DocumentForm(request.POST、request.FILES)将创建一个Document实例,它将验证并存储正确的值。

这太棒了,我尝试了,效果非常好,我需要UUIDField来处理其他内容。不管怎样,谢谢这太棒了,我试过了,效果很好,我需要UUIDField来做其他的东西。无论如何,谢谢