Django | Save Model Select List

I have a form that has a select list:

<select name="cellSerpro" id="idcellserpro" class="field text" >
    <option value="">---</option>
    <option value="option1">Verizon</option>
    <option value="option2">AT&T</option>
    <option value="option3">T-Mobile</option>
    <option value="option4">Sprint</option>
</select>

So, how do I get the selected value from the Django model class to store it in the database, I have a network search, but could not find a way to do this.

My sample model:

class Author(models.Model):
    first_name = models.CharField(max_length=100)
    last_name = models.CharField(max_length=3)
    cellSerpro = # ---- how to declare the choice list and get the selected value ----

Thanks..

+3
source share
1 answer

You must use ModelForms .

(updated)

1) In your models.py you define the choice:

CELLSERPRO_CHOICES = (
    ('ver', 'Verizon'),
    ('att', 'AT&T'),
    ('tmo', 'T-Mobile'),
    ('spr', 'Sprint'),
)

2) In your models.py inside the "class Author" you define the cellSerpro field as follows:

class Author(models.Model):
    cellSerpro = models.CharField(max_length=3, choices=CELLSERPRO_CHOICES)

3) In your forms.py (create it if you don't have one), you define the form as follows:

class AuthorForm(ModelForm):
    class Meta:
        model = Author

4) , .

+7

Source: https://habr.com/ru/post/1741916/


All Articles