Django新手在这里。
我有一个带有DateTimeField的表单,该表单不能为空,但是尽管给出了时间和日期,但我收到错误。
错误讯息:
IntegrityError at /app/new_action/
app_action.event_time may not be NULL
示例输入:
02/08/2014 15:00
models.py
class Action(models.Model):
name = models.CharField(max_length=200)
description = models.CharField(max_length=200)
location = models.CharField(max_length=200)
event_time = models.DateTimeField()
created_by = models.ForeignKey(UserProfile)
last_modified = models.DateTimeField('Last Modified')
def __unicode__(self):
return self.name
forms.py
class ActionForm(forms.ModelForm):
event_time = forms.DateTimeField(input_formats=['%m/%d/%Y %H:%M'])
class Meta:
model = Action
fields = ('name','description','location',)
views.py
@login_required
def new_action(request):
context = RequestContext(request)
if request.method == 'POST':
form = ActionForm(request.POST)
if form.is_valid():
form.save(commit=True)
#note = form.save(commit=True)
#note.created_by = profile.id
#note.save()
return index(request)
else:
print form.errors
else:
form = ActionForm()
额外注意:如果可能,我想保留此输入格式。为了测试我将字段设置回DateTimeField,但是如果我能解决这个问题,我将回到使用this,它会产生这种输入格式。
答案 0 :(得分:0)
如Alvaro所述,请尝试将forms.py更改为
class ActionForm(forms.ModelForm):
event_time = forms.DateTimeField(input_formats=['%m/%d/%Y %H:%M'])
class Meta:
model = Action
fields = ['name','description','location', 'event_time']