我有一个非常简单的模型来选择以我的语言显示一周中的几天,就像这样:
class Day(models.Model):
DAY_OF_WEEK = (
('Monday', 'Poniedziałek'),
('Tuesday', 'Wtorek'),
('Wednesday', 'Środa'),
('Thursday', 'Czwartek'),
('Friday', 'Piątek'),
('Saturday', 'Sobota'),
('Saturday', 'Niedziela')
)
day_week = models.CharField(max_length=15, choices=DAY_OF_WEEK, default=date.today)
如何在表单中自动将字段设置为星期几?根据我的期望(default=date.today
),将其添加到模型中无法正常工作。任何帮助将不胜感激。
答案 0 :(得分:0)
尝试一下。定义一个函数以获取week_day
def get_default():
week_day = date.today().weekday()
if week_day == 0:
return "Monday"
if week_day == 1:
return "Tuesday"
if week_day == 2:
return "Wednesday"
if week_day == 3:
return "Thursday"
if week_day == 4:
return "Friday"
if week_day == 5:
return "Saturday"
if week_day == 6:
return "Sunday"
class Day(models.Model):
#####################
# model fields....
#####################
week_day = models.CharField(max_length=15, default=get_default)