Reputation: 99
I am trying to submit data from a checklist using forms. The checklist is generated from a database table and after submission the checked items are inserted into another database table. I am getting the error as form is invalid.
This is my models.py CustomStudent is the database from where I am taking value and Reports is the database where I am submitting values
class CustomStudent(models.Model):
_id = models.AutoField
sname = models.CharField(max_length = 50)
slname = models.CharField(max_length = 50)
password = models.CharField(max_length = 255, default = '')
def __str__(self):
return str(self.slname)
return str(self.sname)
class Report(models.Model):
# _id = models.AutoField()
tname = models.CharField(max_length = 100)
sname = models.CharField(max_length = 100)
fdate = models.DateField()
tdate = models.DateField()
dailydate = models.DateField()
objective = models.CharField(max_length = 512)
tplan = models.CharField(max_length = 512)
how = models.CharField(max_length = 512)
material = models.CharField(max_length = 512)
extra = models.CharField(max_length = 512)
topic = models.CharField(max_length = 512)
pftd = models.CharField(max_length = 512)
activity = models.CharField(max_length = 512)
comment = models.CharField(max_length = 512)
thought = models.CharField(max_length = 512)
admin_comment = models.CharField(max_length = 255)
def __str__(self):
return str(self.tname)
return str(self.sname)
This is code from my forms.py to use the database.
sname = forms.ModelMultipleChoiceField(queryset=CustomStudent.objects.all().values_list('sname', flat=True), required = False, widget =forms.CheckboxSelectMultiple( attrs ={'class':' form-check-input' ' form-check-inline'}))
This error is generated due to the value of the checklist string is returning in double quotes. ex. "student" from 1st database.
I have tried splitting string to remove double quotes but even that doesn't help.
Upvotes: 1
Views: 26
Reputation: 16505
The field forms.ModelMultipleChoiceField
needs a model for the choices.
If you want to assign strings, you can use forms.MultipleChoiceField
instead.
sname = forms.MultipleChoiceField(
# list of tuples; the first of each pair is the value to be
# returned when selected, the second is the value to be displayed
choices=[
(e, e)
e for e in CustomStudent.objects.all().values_list('sname', flat=True)],
required=False,
widget=forms.CheckboxSelectMultiple(
attrs={'class':' form-check-input' ' form-check-inline'}))
See detailed description of the attribute choices
.
Upvotes: 1