1

I want to render a form with some checkboxes. Those checkboxes have a popover with additional information, which is stored in the database. How can I access that additional data in a form template?

Should I do that in the View while doing: form = SomeForm(what goes here?)?

Does it belong inside the Form Template?

I'm a bit lost here, maybe I just need an example.

models.py:

class SomeModel(models.Model):
    name = models.CharField('Model name', unique=True, validators=[MinLengthValidator(1)], max_length=200)
    stuff = models.ForeignKey('Stuff',related_name='stuff')

    def __str__(self):
        return self.name

class Stuff(models.Model):
    name = models.CharField('Stuffname', unique=True, validators=[MinLengthValidator(1)], max_length=200)
    info = models.TextField('Info', validators=[MinLengthValidator(1)], max_length=300)

    def __str__(self):
        return self.name

forms.py:

class SomeForm(ModelForm):
    stuff = ModelChoiceField(queryset=Stuff.objects.all(), empty_label=None)
    def __init__(self, *args, **kwargs):
        super(SomeForm, self).__init__(*args, **kwargs)

        self.helper = FormHelper(self)
        self.helper.layout = Layout(
            'name',
            InlineCheckboxes('stuff'),
            Submit('save', 'save')
        )
    class Meta:
        model = Stuff
        fields = '__all__'

CustomForm.html:

<div class="controls {{ field_class }}"{% if flat_attrs %} {{ flat_attrs|safe }}{% endif %}>
{% include 'bootstrap3/layout/field_errors_block.html' %}

{% for choice in field.field.choices %}

  {% if not inline_class %}<div class="checkbox">{% endif %}
    <label class="checkbox-img {% if inline_class %}checkbox-{{ inline_class }}{% endif %}
data-toggle="popover" data-trigger="hover" data-placement="top" data-title="{{choice.0}}" data-html="true" data-content="How to get the data in here? ">
        <input type="checkbox"{% if choice.0 in field.value or choice.0|stringformat:"s" in field.value or choice.0|stringformat:"s" == field.value|stringformat:"s" %} checked="checked"{% endif %} name="{{ field.html_name }}" id="id_{{ field.html_name }}_{{ forloop.counter }}" value="{{ choice.0|unlocalize }}" {{ field.field.widget.attrs|flatatt }}>
    </label>
  {% if not inline_class %}</div>{% endif %}
{% endfor %}

{% include 'bootstrap3/layout/help_text.html' %}

How can I parse the value from Stuff.info inside the data-content attribute?

Solved:

Just had to use the queryset property from the field.

CustomForm.html:

<div class="controls {{ field_class }}"{% if flat_attrs %} {{ flat_attrs|safe }}{% endif %}>
{% include 'bootstrap3/layout/field_errors_block.html' %}

{% for choice in field.field.queryset%}

  {% if not inline_class %}<div class="checkbox">{% endif %}
    <label class="checkbox-img {% if inline_class %}checkbox-{{ inline_class }}{% endif %}
data-toggle="popover" data-trigger="hover" data-placement="top" data-title="{{choice.name}}" data-html="true" data-content={{ choice.info }}">
        <input type="checkbox"{% if choice.id in field.value or choice.id|stringformat:"s" in field.value or choice.id|stringformat:"s" == field.value|stringformat:"s" %} checked="checked"{% endif %} name="{{ field.html_name }}" id="id_{{ field.html_name }}_{{ forloop.counter }}" value="{{ choice.id|unlocalize }}" {{ field.field.widget.attrs|flatatt }}>
    </label>
  {% if not inline_class %}</div>{% endif %}
{% endfor %}

{% include 'bootstrap3/layout/help_text.html' %}

qwertasyx
  • 25
  • 8
  • Have a look at the solutions [suggested here](http://stackoverflow.com/questions/10300685/how-to-get-modelchoicefield-instances-in-the-template). – solarissmoke May 01 '16 at 03:49
  • Thanks, you gave me the right direction. Field.queryset was the way to go. i have to use field.field.queryset and i dont really understand why... but works for me :) – qwertasyx May 01 '16 at 13:12

0 Answers0