how to add widgets "form-control" in django-money

Viewed 136

how to add widgets in django field for currency and currency code ? i want to add class "form control" in that field.

django-money in templates

templates.html

<div class="col-sm-4">
    <label for="jumlahUsulanDana">Jumlah Usulan Dana</label>
        {{form.jumlah_dana_usulan}}
</div>

forms.py

class Meta:
    model           = UserUsulan
    exclude         = ['status_usulan' ,'jumlah_dana_disetujui', 
   'keterangan_operator', 'com_reviewer_1', 'com_reviewer_2' ]
    widgets = {'jumlah_dana_usulan'        : forms.TextInput({'class' : 
                'form-control', 'id' : 'jumlahUsulanDana', 'placeholder' 
                : 'Jumlah Usulan Dana'}),
              }

EDIT

If Used attrs in forms.py, any idea ? Used attrs

models.py

from djmoney.models.fields import MoneyField

class UserUsulan(models.Model):
     ...
     jumlah_dana_usulan          = MoneyField(max_digits=14, decimal_places=2, default_currency='IDR')
     ...
1 Answers

Well you are using the django-money library link here. This library uses a multiwidget for this component see here what I can recomend you is just customize the main widget

Do the following:

from django.conf import settings
from django-money.forms.widgets import MoneyWidget


class Meta:
    # ... Your actually code.
    widgets = {
        'jumlah_dana_usulan': MoneyWidget(
            amount_widget=forms.TextInput(attrs={'class':'form-class'}),
            currency_widget=forms.Select(attrs={
                    'class':'form-control',
                    'placeholder':'Jumlah Usulan Dana',
                    'id' : 'jumlahUsulanDana',
                },
                choices=settings.CURRENCY_CHOICES,
            ),
        ),
        # ... Here more widget fields
    }

The main point in here is that you have to explore how the base widget is working and try to change the widget of that Multiwidget passing through the constructor.

Related