0
我目前正在使用Django1.7,並且正在處理一個需要我根據值顯示特定表單域的項目。這是得到的。我如何從views.py中更改ModelForm中字段的屬性?
forms.py
class ServiceForm(forms.ModelForm):
area = forms.ModelChoiceField(queryset=Area.objects.all(), label=u'Area')
class Meta:
model = Service
fields = ['title', 'price', 'negotiable', 'location', 'phone', 'email', 'area', 'category', 'description',
'image1', 'image2', 'image3', 'image4']
widgets = {
'title': forms.TextInput(attrs={'placeholder': 'Service title e.g: Servicing of generators'}),
'location': forms.TextInput(attrs={'placeholder': 'Physical location of where the service will be rendered'}),
'price': forms.NumberInput(attrs={'placeholder': 'item price in figures e.g: 2000, 0 if free'}),
'phone': forms.NumberInput(attrs={'placeholder': 'seller\'s phone number (buyers can contact)'}),
'email': forms.EmailInput(attrs={'placeholder': 'seller\'s email (buyers can contact)'}),
'description': forms.Textarea(attrs={'placeholder':'Service description, a good and straight description attracts more interest', 'rows':'4'}),
}
def save(self, commit=True):
service = super(ServiceForm, self).save(commit=False)
service.active = True
if service.price == '':
service.price = 0.0
if commit:
service.save()
return service
是有辦法,我可以屬性 '區域' 的財產從我views.py改變?
究竟是什麼你想改變什麼? – rnevius 2015-04-02 09:44:05
是的,我想從我的views.py – phourxx 2015-04-02 09:52:37
改變'區域'的查詢集。你可以在窗體的'__init __()'方法中覆蓋它。 – Rohan 2015-04-02 10:17:00