django-bootstrap-v5 DecimalField Throwing Errors in ModelForm Class


Problem:

Using the django-bootstrap-v5 module, I get the error:

"init() got an unexpected keyword argument 'attrs'"

on a DecimalField Widget in the definition of a ModelForm class for a simple Model. The Model is defined as follows:

class MenuItem(models.Model):
    menuname=models.CharField(max_length=20,primary_key=True)
    description=models.TextField()
    price=models.DecimalField(max_digits = 5, decimal_places = 2)

And the Model Form is defined as follows:

class MenuItemForm(forms.ModelForm):
    class Meta:
        model=MenuItem
        fields='__all__'
        widgets = {'menuname': forms.TextInput(attrs={'class': 'form-control'}),
                'description': forms.Textarea(attrs={'class': 'form-control'}),
                'price': forms.DecimalField(attrs={'class': 'form-control'}),
                }

The error is thrown on the Line:

'price': forms.DecimalField(attrs={'class': 'form-control'}),

If I change that line to:

'price': forms.TextInput(attrs={'class': 'form-control'}),

Then no error it thrown. But of course I would rather use a DecimalField widget rather than a TextInput to render the underlying DecimalField in the model.

Once again this is using the django-bootstrap-v5 module.

Any Ideas?


Solution:

forms.DecimalField is a form field not a widget. The default widget used by DecimalField is NumberInput [Django docs] hence you should use that instead:

class MenuItemForm(forms.ModelForm):
    class Meta:
        model = MenuItem
        fields = '__all__'
        widgets = {'menuname': forms.TextInput(attrs={'class': 'form-control'}),
                'description': forms.Textarea(attrs={'class': 'form-control'}),
                'price': forms.NumberInput(attrs={'class': 'form-control'}),
                }

Recent Tips

  1. update and delete in realm xamarin
  2. Grabbing text between Processing Instructions in XSLT
  3. Foreign key refering to multiple tables
  4. Run a visual studio 2010 Windows7 project on a Windows XP machine
  5. How to apply groupname to HTML radio buttons in asp.net?
  6. What is the most stable modal dialog implementation across browsers to use from javascript for a web app?
  7. Move, Remove, and Replace information from Google Sheets with new entries using Google Script
  8. How can I get rid of jerkiness in WinForms scrolling animation?
  9. Update MySQL table column from another table entities
  10. Shoot fireball once in unity C#
  11. Adding a table of contents to a Microsoft Word Document using vbs
  12. Change locale in android app (onto Hindi)
  13. How do I stop selenium automation if 20% or 1st 20 test cases test methods are failed?
  14. Sharepoint with silverlight app
  15. What's the best way to write robots.txt for github pages using multiple repos?
  16. Biopython: Cant use .count() for biopython
  17. How can I find out the token balance of an address?
  18. ref value is undefined in vue (modal, textarea, $refs)
  19. Azure - HDInsight Hbase Data Insertion Failed
  20. SignalR overwriting OnConnected(), OnDisconnected()
  21. DatePickerDialog displays with two borders
  22. "type 'double' is not a subtype of type 'int' in type cast" error in flutter. What should i do?
  23. hiding the autocomplete list when user click outside the textbox is not working as expected
  24. JSF IceFaces basic problem with redisplaying input value
  25. How to validate material ui TextField in reactjs?
  26. Go and MongoDB connection won't work with panic log "no reachable server"
  27. WordPress Posts Pagination Not Working
  28. F# sprintf won't print in interactive console
  29. Spring Integration get FTP files recursively with outbound-gateway
  30. Jade mixins not getting working from external file