I'm kind of new in Django and I'm trying to fill back a listbox with the data previously saved in database. So when I display the HTML page, the choices initially stored in database will be highlighted. The listbox data are saved in this format ['Oranges','Cantaloupes'] but when I call back my form in my HTML page, everything is filled back with the database data except for the listbox.
Do I have to add something to retrieve the data stored ?
forms.py
FRUIT_CHOICES= [
('Oranges', 'Oranges'),
('Cantaloupes', 'Cantaloupes'),
('Mangoes', 'Mangoes'),
('Honeydews', 'Honeydews'),
]
# Create Add Record Form
class AddRecordForm(forms.ModelForm):
first_name = forms.CharField(required=True, widget=forms.widgets.TextInput(attrs={"placeholder":"First Name", "class":"form-control"}), label="")
last_name = forms.CharField(required=True, widget=forms.widgets.TextInput(attrs={"placeholder":"Last Name", "class":"form-control"}), label="")
email = forms.CharField(required=True, widget=forms.widgets.TextInput(attrs={"placeholder":"Email", "class":"form-control"}), label="")
phone = forms.CharField(required=True, widget=forms.widgets.TextInput(attrs={"placeholder":"Phone", "class":"form-control"}), label="")
address = forms.CharField(required=True, widget=forms.widgets.TextInput(attrs={"placeholder":"Address", "class":"form-control"}), label="")
city = forms.CharField(required=True, widget=forms.widgets.TextInput(attrs={"placeholder":"City", "class":"form-control"}), label="")
state = forms.CharField(required=False, widget=forms.widgets.TextInput(attrs={"placeholder":"State", "class":"form-control"}), label="")
zipcode = forms.CharField(required=True, widget=forms.widgets.TextInput(attrs={"placeholder":"Zipcode", "class":"form-control"}), label="")
regarde = forms.CharField( widget=forms.widgets.Select(choices=FRUIT_CHOICES))
testrever = forms.MultipleChoiceField(choices=FRUIT_CHOICES)
class Meta:
model = Record
exclude = ("user",)
models.py
class Record(models.Model):
created_at = models.DateTimeField(auto_now_add=True)
first_name = models.CharField(max_length=50)
last_name = models.CharField(max_length=50)
email = models.CharField(max_length=100)
phone = models.CharField(max_length=15)
address = models.CharField(max_length=100)
city = models.CharField(max_length=50)
state = models.CharField(max_length=50)
zipcode = models.CharField(max_length=20)
regarde = models.CharField(max_length=50)
testrever = models.CharField(max_length=50)
HTML
<div >
<h1>Update Record </h1>
<br/>
<p >
<form >
{% csrf_token %}
{{ form }}
<br/>
<button type="submit" class="btn btn-secondary">Update Record</button>
</form>
</div>
I found the answer thanks to Willem Van Onsem hints in comments.
I changed CharField
to JSONField
in models.py :
class Record(models.Model):
created_at = models.DateTimeField(auto_now_add=True)
first_name = models.CharField(max_length=50)
last_name = models.CharField(max_length=50)
email = models.CharField(max_length=100)
phone = models.CharField(max_length=15)
address = models.CharField(max_length=100)
city = models.CharField(max_length=50)
state = models.CharField(max_length=50)
zipcode = models.CharField(max_length=20)
regarde = models.CharField(max_length=50)
testrever = models.JSONField(max_length=50)