我正在使用带有postgres的Django 1.6。
我已经阅读了有关设置DATE_INPUT_FORMATS的其他问题,使用input_formats并尝试了几种组合,但start_date字段仍然无法通过以下方式验证:
[u"'09/05/2014' value has an invalid format. It must be in YYYY-MM-DD HH:MM[:ss[.uuuuuu]][TZ] format."]
我尝试将USE_L10N更改为False,使用表单中的input_formats,将模型从DateTimeField更改为DateTime,甚至将格式添加到窗口小部件,但无济于事。以下是当前设置。我觉得这是一个简单的错误,但我无法弄清楚我做错了什么。如何让Django接受输入'09 / 05/2014'?
settings.py
TIME_ZONE = 'America/New_York'
USE_I18N = True
USE_L10N = True
USE_TZ = True
DATETIME_INPUT_FORMATS = ('%m/%d/%Y','%Y/%m/%d','%Y-%m-%d',)
DATE_INPUT_FORMATS = ('%m/%d/%Y','%Y/%m/%d','%Y-%m-%d',)
models.py
...
class RecurringTransaction(models.Model):
user = models.ForeignKey(User)
amount = models.CharField(max_length=20)
account = models.ForeignKey(Account)
category = models.ForeignKey(Category)
interval = models.CharField(choices=INTERVAL_CHOICES, default="months", max_length=10)
occurrences = models.IntegerField()
start_date = models.DateTimeField()
end_date = models.DateTimeField(null=True, blank=True)
active = models.BooleanField(default=True)
repeats = models.IntegerField(choices=REPEAT_CHOICES, default=1)
end_type = models.IntegerField()
forms.py
class RecurringTransactionForm(forms.ModelForm):
start_date = forms.DateField(input_formats=['%m/%d/%Y','%Y/%m/%d','%Y-%m-%d',])
end_date = forms.DateField(required=False, input_formats=['%m/%d/%Y','%Y/%m/%d','%Y-%m-%d',])
class Meta:
model = RecurringTransaction
fields = ('amount','category','interval','occurrences','start_date','end_date','active','repeats','end_type')
widgets = {
'interval': forms.Select(attrs={'class': 'form-control', 'id':'interval'}),
'repeats': forms.Select(attrs={'class': 'form-control', 'id':'repeats'}),
'category': forms.Select(attrs={'class': 'form-control', 'id':'category_id'}),
}
views.py
...
def add_edit_recurrence(request, id=None):
context = RequestContext(request)
if request.method == 'POST':
recurring_trx = RecurringTransaction.objects.filter(id=id).first()
recurring_trx_form = RecurringTransactionForm(request.POST, instance=recurring_trx)
#print recurring_trx_form.errors
if recurring_trx_form.is_valid():
recurring_trx_form.save()
else:
recurring_trx_form = RecurringTransactionForm()
return render_to_response("recurrence_new.html",{"recurring_trx_form":recurring_trx_form}, context)
答案 0 :(得分:0)
您是否需要start_date为DatetimeField?...如果您只想捕获日期,请尝试使用DateField。
答案 1 :(得分:0)
您可以像这样更改表单:
class RecurringTransactionForm(forms.ModelForm):
'''
I have added a class with each date/datetime field so that I could attach Jquery date picker to those classes.Its unnecessary if you don't need it.
'''
def __init__(self,*args, **kwargs):
super(RecurringTransactionForm, self).__init__(*args, **kwargs)
self.fields['start_date'] = forms.CharField(widget=forms.TextInput(attrs={'class': 'date-selector'}))
self.fields['end_date'] = forms.CharField(widget=forms.TextInput(attrs={'class': 'date-selector'}))
def clean_start_date(self):
return datetime.datetime.strptime(self.cleaned_data['start_date'], '%m-%d-%Y')
def clean_end_date(self):
return datetime.datetime.strptime(self.cleaned_data['end_date'], '%m-%d-%Y')
class Meta:
model = RecurringTransaction
fields=['active', 'category',... ] #ignore start_date and end_date from this list