【问题标题】:Django RadioButton as a Foreign KeyDjango RadioButton 作为外键
【发布时间】:2014-10-02 15:09:30
【问题描述】:

美好的一天!

我在 MySQL 数据库中保留了单选按钮的选项。我希望这些选项以表单的形式出现在我的单选按钮上。有时我想打开和编辑我的表单和条目。因此,当我打开现有条目时,我希望看到正确的单选按钮被选中。我该怎么做?

我有以下型号:

class SomeType(models.Model):
    type_title = models.CharField(max_length=200)

class SomeModel(models.Model):
    name = models.CharField(max_length=200)
    type = models.ForeignKey(SomeType)

class SomeForm(forms.ModelForm):
    class Meta:
       model = SomeModel
       fields = ['name', 'type']

这是一个视图示例:

def some_view(request, _id):
    entry = SomeModel.objects.values().get(id=_id)
    form = SomeForm(entry)
    return render_to_response("/myapp/some.html", {'sometypes': SomeType.objects.all(), 'form': form})

最后是我的表单模板的一部分:

{% for field in form %}
  {% if field.name == 'sometype' %}
    {% for sometype in sometypes %}
      {% if forloop.counter == field.value %}
        <input type="radio" name="sometype" id="id_sometype_{{ sometype.id }}" value="{{ sometype.id }}" checked="">
      {% else %}
        <input type="radio" name="sometype" id="sometype_{{ sometype.id }}" value="{{ sometype.id }}">
      {% endif %}
      {{ sometype.type_title }}
    {% endfor %}    
  {% endif %}
{% endfor %}

这里有什么问题? 如何在与我的数据库中相同的收音机中为该条目 (_id) 呈现一个已选中的表单

【问题讨论】:

    标签: python django radio-button foreign-keys modelform


    【解决方案1】:

    您可以覆盖表单中的小部件:

    class SomeForm(forms.ModelForm):
        class Meta:
            model = SomeModel
            fields = ('name', 'type')
    
        def __init__(self, *args, **kwargs):
            super(SomeForm, self).__init__(*args, **kwargs)
            self.fields['type'].widget = forms.RadioSelect
    

    让 Django 像往常一样渲染表单:

    {{ form.as_p }}
    

    如果你想手动渲染无线电输入,你将不得不这样做(is_radio 过滤器是我的自定义过滤器):

    {% for field in form.visible_fields %}
        {% if field|is_radio %}
            <p{% if field.field.required %} class="required"{% endif %}>{{ field.label }}:</p>
            {% for choice in field.field.choices %}
                <div class="radio">
                    <label for="{{ field.auto_id }}_{{ forloop.counter }}" class="control-label">
                        <input type="radio" name="{{ field.name }}" id="{{ field.auto_id }}_{{ forloop.counter }}" value="{{ choice.0 }}"{% if field.value == choice.0 %} checked{% endif %}> {{ choice.1 }}
                    </label>
                </div>
            {% endfor %}
            {% if field.errors %}
                <ul class="list-unstyled text-danger">
                    {% for err in field.errors %}<li>{{ err }}</li>{% endfor %}
                </ul>
            {% endif %}
        {% else %}
            ...
    

    但我不建议使用RadioSelect 作为外键,列表可能会很长。如果您只有几个选择并且很少修改数据,我建议您删除SomeType 模型并改用type = models.CharField(choices=SOME_CHOICES)

    【讨论】:

      猜你喜欢
      • 2016-04-02
      • 2020-07-12
      • 2018-07-04
      • 2020-05-20
      • 2020-08-29
      • 2011-05-21
      • 1970-01-01
      • 2020-08-27
      • 2015-09-26
      相关资源
      最近更新 更多