Reputation: 1412
have a model and have used it to create model formsets . But i see that formset gives id to each columns like form 0 will have id_0_exp_date then id_1_exp_date etc but i am looking if there s a way to set class to the columns so all exp_date formsets have same class . I want this behaviour so i can specify jquery and css on these query easily . I couldnt find this in documentation but i feel there should be a way to do that ..
models.py :
class Expenditure(models.Model):
exp_date = models.DateField("Expenditure_Date")
description = models.CharField(max_length=500)
amount = models.FloatField(default=0)
currency = models.CharField(max_length=15,default="USD",editable=True)
class Meta:
unique_together = ('exp_date', 'description',)
def __unicode__(self):
return self.description
forms.py
class ExpenditureForm(forms.ModelForm):
exp_date = forms.DateField(widget=AdminDateWidget)
description = forms.CharField(max_length=500)
amount = forms.FloatField(initial=0)
currency = forms.CharField(widget=forms.HiddenInput(), initial="USD")
"""
def __init__(self, *args, **kwargs):
super(ExpenditureForm, self).__init__(*args, **kwargs)
if self.instance.id:
self.fields['currency'].widget.attrs['readonly'] = True
"""
# An inline class to provide additional information on the form.
class Meta:
# Provide an association between the ModelForm and a model
model = Expenditure
Thanks in advance
Basically I want to make currency field readonly but seems its not working due to some issues or may be i am doing something wrong :
what i tried : 1. currency = forms.CharField(widget=forms.HiddenInput(attrs={'class':'currencyClass'}), initial="USD") 2. self.fields['currency'].widget.attrs['readonly'] = True in form class .
Upvotes: 0
Views: 346
Reputation: 1951
You can specify attributes in your widgets in the forms :
class ExpenditureForm(forms.ModelForm):
exp_date = forms.DateField(widget=AdminDateWidget(attrs={'class':'nameOfYourClass'}))
See the full documentation here : https://docs.djangoproject.com/en/dev/ref/forms/widgets/
Upvotes: 1