Django MultipleChoiceField callback

53 Views Asked by At

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>
2

There are 2 best solutions below

0
On

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)
0
On

The best solution to this is to install a django-multiselect module so you can use it in your django apps. With that, you can store multiple choice data and it is changeable. You can have something like this: Note: It is important that you add "multiselectfield" to your installed apps.


# .....
MY_CHOICES2 = ((1, 'Item title 2.1'),
               (2, 'Item title 2.2'),
               (3, 'Item title 2.3'),
               (4, 'Item title 2.4'),
               (5, 'Item title 2.5'))

class MyModel(models.Model):

    # ....

    my_field2 = MultiSelectField(choices=MY_CHOICES2,
                                 max_choices=3,
                                 max_length=3)```