[Fixed]-Django: How to render non-Boolean database rows as checkboxes?

0👍

Found what I was looking for. In my form:

LINEITEM_CHOICES = [[x.id, x.descr] for x in LineItems.objects.all()]

class LineItemsForm(forms.Form):
    food = forms.MultipleChoiceField(choices=LINEITEM_CHOICES,
    widget=forms.CheckboxSelectMultiple(), required=False)

In my view:

{% for radio in lineItems.food %}
    <div class="food_radios">
    {{ radio }}
    </div>
{% endfor %}

My code is based off of this post.

Thank you for your answers.

1👍

Ok, then you should design your model this way:

class Product(models.Model):
    description = models.TextField()
    cost = models.CharField(max_length=100) 
    # 'cost' should be a CharField because you would want to save currency name also e.g. €

users dont need a checkbox to buy it, I think, more appropriate way would be just an html button called “put into cart”, where users can put items into cart .. and then in cart page, you give users the chance to delete the selected items again. now you think further about next steps..

0👍

in the model form you can specify in the meta class

class Meta:
    widgets = {
        'descr': CheckboxInput()
    }

something like that anyway.
CheckboxInput is in django.forms.widgets I believe

Leave a comment