开发者

Django | Save model's choice list values

I have a form that has a choice list:

<select name="cellSerpro" id="idcellserpro" class="field text" >
    <option value="">---</option>
    <option va开发者_JS百科lue="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 of it from the Django's model class in order to save it in the database, I have search through the net but couldn't find any way of doing it..

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.. Thanks..


You should use ModelForms.

(updated)

1) In your models.py, you define the choices:

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

2) In your models.py, inside "class Author", you define the cellSerpro field like this:

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 it), you define a form like this:

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

4) And then, just use that form in a view, as you would with any other form.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜