0

我正在尝试将用户选择的选项(选项)保存在数据库中,并使用数据库中保存的数据填充(检查/选择)相同的选项。

模型:

class ScheduledEvent(models.Model):

event_id            = models.IntegerField()
project_id          = models.ForeignKey(Project, db_column = 'project_id')
start_date          = models.DateTimeField(null = False, default = timezone.now)
end_date            = models.DateTimeField(null = False, default = timezone.now)
next_scheduled_date = models.DateTimeField(default = timezone.now)
include_week_day    = models.CharField(max_length=200)
include_month_day   = models.CharField(max_length=200)

剩下的代码:

weekdays=(('Mon',"Monday"),
        ('Tue','Tuesday'),
        ('Wed','Wednesday'),
        ('Thr','Thrusday'),
        ('Fri','Friday'),
        ('Sat','Saturday'),
        ('Sun','Sunday'))


class CheckBoxdays(forms.ModelForm):
    model = ScheduledEvent
    included_week_day = forms.MultipleChoiceField(choices=weekdays, widget=forms.CheckboxSelectMultiple)

选项被保存在数据库中,但选项(选项)没有从存储在数据库中的数据中填充。

但是以下代码可以正常工作:

weekdays=(('1',"Monday"),
        ('2','Tuesday'),
        ('3','Wednesday'),
        ('4','Thrusday'),
        ('5','Friday'),
        ('6','Saturday'),
        ('7','Sunday'))

class CheckBoxdays(forms.ModelForm):
    model = ScheduledEvent
    include_week_day = forms.MultipleChoiceField(choices=weekdays, widget=forms.CheckboxSelectMultiple)

The options are populated when the first value in the choice tuple is be a single character. 我在这里做错了吗?

4

1 回答 1

0

您必须使用 MultipleChoiceFile 的模型版本来执行您想要执行的操作:

class CheckBoxdays(forms.ModelForm):
    model = ScheduledEvent
    include_week_day = forms.ModelMultipleChoiceField(choices=weekdays, widget=forms.CheckboxSelectMultiple)
于 2015-05-28T13:17:37.587 回答