python - How to save checked checkboxes in a CheckboxSelectMultiple field in an edit form in Django -


i made form model, , want create update form model, i'm having trouble checkboxselectmultiple field. here code:

choices.py

frequency_choices = (('sunday', 'sunday'), ('monday', 'monday'), ('tuesday', 'tuesday'), ('wednesday', 'wednesday'), ('thursday', 'thursday'), ('friday', 'friday'), ('saturday', 'saturday')) 

models.py

class schedules(models.model):     course_name = models.foreignkey(course)     location = models.charfield(max_length=128, choices=location_choices, default='south_plainfield')     room = models.charfield(max_length=128, choices=room_choices, default='a')     start_date = models.datefield(auto_now=false, auto_now_add=false, default=datetime.date.today)     start_time = models.charfield(max_length=128, choices=start_time_choices, default='eight-thirty am')     end_time = models.charfield(max_length=128, choices=end_time_choices, default='eight-thirty am')     instructor = models.foreignkey(instructor)     total_hours = models.charfield(max_length=128, choices=total_hours_choices, default='six')     # relevant code here     frequency = models.charfield(max_length=128)     status = models.charfield(max_length=128, choices=status_choices)     interval = models.charfield(max_length=128, choices=interval_choices, default='1 day')     initiated_by = models.charfield(max_length=128, null=true)     schedule_id = models.integerfield(default=0) 

forms.py

class scheduleform(forms.modelform):     course_name = coursechoicefield(queryset=course.objects.filter(status=true), label="course name", widget=forms.select(attrs={'class': 'form-control'}))     location = forms.choicefield(choices=location_choices, initial='south_plainfield', label="location", widget=forms.select(attrs={'class': 'form-control'}))     room = forms.choicefield(choices=room_choices, initial='a', label="room", widget=forms.select(attrs={'class': 'form-control'}))     start_date = forms.datefield(input_formats=['%m/%d/%y'], label="start date", widget=dateinput(format='%m/%d/%y'), help_text="mm/dd/yyyy")     start_time = forms.choicefield(choices=start_time_choices, initial='eight-thirty am', label="start time", widget=forms.select(attrs={'class': 'form-control'}))     interval = forms.choicefield(choices=interval_choices, initial='1 day', label="interval", widget=forms.select(attrs={'class': 'form-control'}))     # hours_per_class = forms.choicefield(choices=hours_per_class_choices, initial='four_and_half', label="hours per class", widget=forms.select(attrs={'class': 'form-control'}))     total_hours = forms.choicefield(choices=total_hours_choices, initial='six', label="total hours", widget=forms.select(attrs={'class': 'form-control'}))     instructor = instructorchoicefield(queryset=instructor.objects.all(), label="instructor", widget=forms.select(attrs={'class': 'form-control'}))     end_time = forms.choicefield(choices=end_time_choices, initial='eight-thirty am', label="end time", widget=forms.select(attrs={'class': 'form-control'}))     # relevant code here     frequency = forms.multiplechoicefield(widget=forms.checkboxselectmultiple, choices=frequency_choices, label="frequency", help_text="please select @ least one")     status = forms.choicefield(widget=forms.radioselect, choices=status_choices, label="status")      class meta:         model = schedules         fields = ('course_name', 'instructor', 'location', 'room', 'start_date', 'start_time', 'end_time', 'interval', 'total_hours', 'frequency', 'status',) 

views.py

def update_schedule(request, pk):     schedule = get_object_or_404(schedules, pk=pk)     form = scheduleform(request.post or none, instance=schedule)     if form.is_valid():         form.save()         return redirect('schedule_list')     return render(request, "schedule/update_schedule.html", {'form':form}) 

update_schedule.html

{% block main_content %} <style>     ul {         list-style: none;     } </style> <h2>update course schedule</h2> <br> <form id="update_schedule_form" method="post" action="">     {% csrf_token %}     <div class="row">         {% field in form %}             <div class="col-gl-4 col-md-4">                 <div class="form-group">                     <strong>{{ field.errors }}</strong>                     {{ field.label_tag }}                     {{ field.help_text }}                     <br>                     {{ field }}                     <script></script>                 </div>             </div>         {% endfor %}     </div>     <button type="submit" name="submit">update schedule</button> </form> {% endblock %} 

for reason, choices checked in frequency field not stay checked when go update form instance, though rest of fields do. know is field accepts multiple choices. curiously, table made returns frequency array. know has been asked before, did not answer, i'm hoping can me dilemma. thanks.

i found answer. apparently supposed change field in model multiselectfield.


Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -