Hiya, DecimalField already sets step based on the number of decimal places
def test_decimalfield_widget_attrs(self): f = DecimalField(max_digits=6, decimal_places=2) self.assertEqual(f.widget_attrs(Widget()), {}) self.assertEqual(f.widget_attrs(NumberInput()), {'step': '0.01'}) f = DecimalField(max_digits=10, decimal_places=0) self.assertEqual(f.widget_attrs(NumberInput()), {'step': '1'}) f = DecimalField(max_digits=19, decimal_places=19) self.assertEqual(f.widget_attrs(NumberInput()), {'step': '1e-19'}) f = DecimalField(max_digits=20) self.assertEqual(f.widget_attrs(NumberInput()), {'step': 'any'}) f = DecimalField(max_digits=6, widget=NumberInput(attrs={'step': '0.01'})) self.assertWidgetRendersTo(f, '<input step="0.01" name="f" type="number" id="id_f" required>') I’m not sure about extending it for FloatField: > On 17 Mar 2021, at 12:35, Josh Smeaton <josh.smea...@gmail.com> wrote: > Is there a better API we can think of for customising widgets from the field > constructor that could then be passed through? As a rough example: > my_step_field = forms.FloatField(widget_attributes={"step": 0.5}) > Thoughts? In general, I think we should be very cautious about adding more API here. There’s already one clear way to do it, passing the widget. Adding (say) a widget_attributes is no clearer than the original: my_step_field = forms.FloatField(widget=NumberInput(attrs={'step': 0.5})). Except now there are TWO ways of configuring one widget attribute. We set maxlength/minlength on widgets for CharFields because they map from an already existing kwarg. For the case of `step` on a FloatField we’d need to add a kwarg — but that’s only there to set a single attribute on the widget. In that case, I don’t think the explicit declaration is too much to ask. “But I need this for all my forms” — create a project-level field and declare it once there. While not all attributes have custom kwargs on the field: "We can set THAT widget attribute from the field, why not THIS one?” tl;dr: I sceptical this would be a good extension of the API surface area. C. -- You received this message because you are subscribed to the Google Groups "Django developers (Contributions to Django itself)" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-developers+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/483FD31B-347C-4A4A-BDEF-52B724795123%40gmail.com.