2016-08-02 8 views
0

私はStackoverflow上の他のソリューションを見てきましたが、何も私が持っている問題に役立つようです。フォームからチェックボックスの値を保存する問題

保存する必要のある情報を記入するためのフォームがあります。私はユーザーに3つのオプションを与え、それらをチェックすることができます。ただし、値が無効であるため、フォームは保存されません。ここで

は私のモデルである:ここでは

from django.db import models 
from django.utils import timezone 


FLAG_CHOICES = (('Active', 'Active'), ('Inactive', 'Inactive'),) 
STATUS_CHOICES=(('Critical', 'Critical'), ('Medium', 'Medium'), ('Low','Low')) 


class Event(models.Model): 
    event_status=models.CharField(max_length=10, choices=STATUS_CHOICES) 
    event_title=models.CharField(max_length=50) 
    event_description=models.CharField(max_length=500) 
    event_flag=models.CharField(max_length=10, choices=FLAG_CHOICES) 
    date_active=models.DateField(default=timezone.now()) 
    time_active=models.TimeField(default=timezone.now()) 

    def __str__(self): 
     return self.event_title 

は私のフォームです:

from django import forms 
from server_status.models import Event 

FLAG_CHECKBOX = [('active', 'Active'), ('inactive', 'Inactive'), ] 
STATUS_CHOICES=[('critical', 'Critical'), ('medium', 'Medium'), ('low','Low'),] 


class Add_Event_Form(forms.ModelForm): 
    event_title = forms.CharField(max_length=50, help_text="Please enter an informative title.") 
    event_status = forms.MultipleChoiceField(choices=STATUS_CHOICES, widget=forms.CheckboxSelectMultiple, 
              help_text="Please select the status of the event") 
    event_description = forms.CharField(max_length=500, initial="", 
             help_text="Enter a short description of the event here") 
    event_flag = forms.MultipleChoiceField(choices=FLAG_CHECKBOX, required=True, widget=forms.CheckboxSelectMultiple, 
              help_text="Please select the status of this event.") 
    date_active = forms.DateField(required=True, widget=forms.DateInput(attrs={'class': 'datepicker'}), 
            help_text="Please select a date for this event.") 
    time_active = forms.TimeField(required=True, widget=forms.TimeInput(format='%HH:%MM'), 
            help_text="Please select a time for this event in HH:MM format.") 


    class Meta: 
     model = Event 
     fields = '__all__' # I want all fields to be editable 

Webページ自体にこのエラーが表示されたら、保存を押したとき:

Select a valid choice. [u'critical'] is not one of the available choices. 

このエラーが来ます'クリティカル'と表示されているボックスにチェックを入れてください。お使いのモデルで

答えて

1

あなたはSTATUS_CHOICESの値(各タプルの最初の値)CriticalMedium、アッパーケースとして最初の文字とLowを持っているが、あなたは、フォーム内のすべての小文字を行いました。モデルの選択肢を変更して、フォーム1と同じSTATUS_CHOICESを使用する必要があります。

関連する問題