2

これに従いましたが、フォームのチェックボックスとしてCHOICESを表示できません。

models.py

class Car(models.Model):
    TYPE_CHOICES = (
       ('s', 'small'),
       ('m', 'medium'),
       ('b', 'big'),
     )
     type = models.CharField(max_length=1, choices=TYPE_CHOICES)

forms.py

from django import forms
from django.forms.widgets import CheckboxSelectMultiple

from cars.models import Car

class AddCar(forms.ModelForm):
    class Meta:
        model = Car
        type = forms.MultipleChoiceField(choices=Car.TYPE_CHOICES, widget=forms.CheckboxSelectMultiple())
4

4 に答える 4

4

その単一の値以来forms.RadioSelect()の代わりに使用する必要があります。forms.CheckboxSelectMultiple()

ModelFormのウィジェットをオーバーライドするには、ドキュメントを確認してください

class AddCar(forms.ModelForm):
    class Meta:
        model = Car
        widgets = {'type': forms.RadioSelect}

またはあなたの質問のように、線は線の上、内側にtypeあるべきですclass MetaAddCar

class AddCar(forms.ModelForm):
    type = forms.ChoiceField(choices=Car.SCENERY_CHOICES, widget=forms.RadioSelect)

    class Meta:
        model = Car
于 2012-06-02T06:51:10.457 に答える
1

使用してRoute.SCENERY_CHOICESいないCar.TYPE_CHOICES

于 2012-06-02T03:41:29.610 に答える
0

それはforms.formsのためです

class AddCarForm(forms.Form):
    type = forms.MultipleChoiceField(required=False,
    widget=forms.CheckboxSelectMultiple, choices=TYPE_CHOICES)

それはforms.ModelFormです

class AddCar(forms.ModelForm):
    type = forms.MultipleChoiceField(required=False,
    widget=forms.CheckboxSelectMultiple, choices=TYPE_CHOICES)

    class Meta:
        model = Car

次に、これを使用するために非常に重要なテンプレートで

{{ form.type }} 

typeつまり、HTMLのように呼び出さないでください<input type="checkbox" name="type" id="id_type">

于 2016-02-07T23:08:54.173 に答える
0

カスタムフィールドを使用せずに複数選択を本当に使用したい場合、これは私が同様のシナリオで行ったことです。警告:データベースに保存されている値は、通常の形式に違反しています。しかし、それは2〜3の値の文字列であると想定されているため(成長する可能性は非常に低いので、このクイックハックを好みました)

私がやったことは、モデルはCharFieldのみを使用し、何に使用されるかを気にする必要はないということです。一方、ModelFormは、複数選択ロジックを処理します。

私の「models.py」で

class Notification(models.Model):
    platforms = models.CharField(max_length=30, blank=False)

「forms.py」で

class NotificationForm(forms.ModelForm):
    class Meta(object):
        model = models.Notification

    platforms = forms.MultipleChoiceField(initial='android', choices=(('ios', 'iOS'), ('android', 'Android')), widget=forms.CheckboxSelectMultiple)

    def __init__(self, *args, **kwargs):
        instance = kwargs['instance']
        # Intercepting the instance kw arg, and turning it into a list from a csv string.
        if instance is not None:
            instance.platforms = instance.platforms.split(",")
            kwargs['instance'] = instance

        super(NotificationForm, self).__init__(*args, **kwargs)
        # Do other stuff

    def clean(self):
        cleaned_data = super(NotificationForm, self).clean()
        platforms = cleaned_data['platforms']
        # Convert the list back into a csv string before saving
        cleaned_data['platforms'] = ",".join(platforms)
        # Do other validations
        return cleaned_data

両方のチェックボックスが選択されている場合、データベース列に保存されるデータは文字列「ios、android」になります。それ以外の場合は、「ios」、「android」になります。私が言ったように、確かに正規化されていません。そして、あなたのフィールドがいつか多くの値を持つことができるならば、物事は実際に醜くなるかもしれません。

于 2018-04-23T20:55:22.250 に答える