0

My model:

class LineItems(models.Model):
    descr = models.CharField(max_length=100)
    cost = models.DecimalField(max_digits=5, decimal_places=2)

My form:

class LineItemsForm(forms.ModelForm):
    class Meta:
        model = LineItems
        fields = ['descr', 'cost']

Can someone please tell me either 1) how to render all of the product rows as checkboxes or 2) the proper way to do this if I'm coming at it incorrectly?

2
  • then why did you define the fields as DecimalField and CharField() Commented Jan 6, 2016 at 5:29
  • I'm not sure how else to define them in a database driven application. I want a database table that contains product descriptions and product costs. I want the user to see the description and the cost and have a checkbox to check if they want to buy it. How else can I accomplish that? I'm (obviously) new to Django, so if I'm coming at this completely wrong, please correct me. Commented Jan 6, 2016 at 5:47

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.