I am allowing someone to set prices for his pizza toppings, and I have a simple form which is the default DecimalField widget
models.py:
class Topping(models.Model):
title = models.CharField(max_length=60)
description = models.CharField(max_length=50, blank=True, null=True)
price = models.DecimalField(max_digits=4, decimal_places=2, default=0.00)
forms.py:
class ToppingForm(forms.ModelForm):
class Meta:
model = Topping
fields = ('title', 'price')
As of now, the widget allows clicking up or down to increase the price, but it defaults to 0.01, or a penny. This is useless, and I want them to be able to jump by 25 cents at a time. I don't know if I'm reading the wrong github, but the source shows nothing of interest, like a keyword arg to set increments:
https://github.com/django/django/blob/master/django/forms/widgets.py
class TextInput(Input):
input_type = 'text'
def __init__(self, attrs=None):
if attrs is not None:
self.input_type = attrs.pop('type', self.input_type)
super(TextInput, self).__init__(attrs)
class NumberInput(TextInput):
input_type = 'number'
Thank you
DecimalField is a field which stores a fixed-precision decimal number, represented in Python by a Decimal instance.
Let's try to use required via Django Web application we created, visit http://localhost:8000/ and try to input the value based on option or validation applied on the Field. Hit submit. Hence Field is accepting the form even without any data in the geeks_field. This makes required=False implemented successfully.
A widget is Django's representation of an HTML input element. The widget handles the rendering of the HTML, and the extraction of data from a GET/POST dictionary that corresponds to the widget. The HTML generated by the built-in widgets uses HTML5 syntax, targeting <!DOCTYPE html> .
I think you can do this by adding the step
attribute to the NumberInput
widget.
class ToppingForm(forms.ModelForm):
class Meta:
model = Topping
fields = ('title', 'price')
widgets = {
'price': forms.NumberInput(attrs={'step': 0.25}),
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With