I have a form that you can select a province and its' cities. This is the model that my form is using in my app:
class ScientificSchedule(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
province = models.CharField(max_length=100, blank=True)
city = models.CharField(max_length=2048, blank=True)
#other fields
and the form:
class ScientificScheduleForm(forms.ModelForm):
class Meta:
model = ScientificSchedule
fields = '__all__'
also these are my views and HTML code:
class ScientificScheduleView(FormView):
model = ScientificSchedule
template_name = 'reg/scientific-schedule.html'
form_class = ScientificScheduleForm
success_url = '/scientific/schedule'
def post(self, request):
form = ScientificScheduleForm(request.POST)
if form.is_valid():
return self.form_valid(form)
else:
return self.form_invalid(form)
def form_valid(self, form):
form.save()
return super(ScientificScheduleView, self).form_valid(form)
and this is part of the form which is for city and province fields:
<form id="manager-users" method="POST" autocomplete="off" class="ant-form ant-form-horizontal">
{% csrf_token %}
<div class="ant-row-flex ant-row-flex-space-between" style="margin-left: -10px; margin-right: -10px;">
<div class="ant-col ant-col-xs-24 ant-col-md-12" style="padding-left: 10px; padding-right: 10px;">
<div class="ant-row ant-form-item formItemRow">
<div class="ant-col ant-form-item-label"><label for="province" class="ant-form-item-required" title="province">province</label></div>
<div class="ant-col ant-form-item-control-wrapper">
<div class="ant-form-item-control has-success">
<span class="ant-form-item-children">
<div id="province" class="ant-select ant-select-selection ant-select-selection--single" style="width: 100%;">
<select name="province" class="ant-col ant-select-selection ant-select-selection--single" role="combobox" aria-autocomplete="list" aria-haspopup="true" tabindex="-1">
<option value="" class=""> - </option>
</select>
</div>
</span>
</div>
</div>
</div>
</div>
<div class="ant-col ant-col-xs-24 ant-col-md-12" style="padding-left: 10px; padding-right: 10px;">
<div class="ant-row ant-form-item formItemRow">
<div class="ant-col ant-form-item-label"><label for="city" class="ant-form-item-required" title="city">city</label></div>
<div class="ant-col ant-form-item-control-wrapper">
<div class="ant-form-item-control has-success">
<span class="ant-form-item-children">
<div id="city" class="ant-select ant-select-selection ant-select-selection--single" style="width: 100%;">
<select multiple name="city" class="ant-col ant-select-selection ant-select-selection--single" role="combobox" aria-autocomplete="list" aria-haspopup="true" tabindex="-1" id="set-multi">
<option value="" class="" selected> - </option>
</select>
</div>
</span>
</div>
</div>
</div>
</div>
<div style="display: flex; justify-content: flex-end; margin-top: 1rem;">
<input id="btn_submit" type="submit" value="submit" class="ant-btn ant-btn-primary" />
</div>
</div>
the cool thing about these fields is that the city field will only show cities inside the chosen province. So, at first, the user cant pick cities, they have to select one province and then choose 1, some, or all of the cities. however, submitting the form only saves the last option I chose for the city. For instance, I choose city1, city4, and city5 but only city5 is saved. how can I save this field like a list? I want to save ['city1','city4','city5'] in the city field of my model. thank you for your responses