Folks,
I need to implement a form that may vary a little depending on a variable. My class that subclasses ModelForms looks like this
class ConstantVwModelForm(forms.ModelForm):
#couple attributes
def __init__(self, hasData, *args, **kwargs):
class Meta:
fields = ('xx', 'yy' ..)
I am looking for the very best way to access the variable hasData
from the inner class Meta
, it would be like
class ConstantVwModelForm(forms.ModelForm):
#couple attributes
def __init__(self, hasData, *args, **kwargs):
class Meta:
if hasData:
fields = ('xx', 'yy', ..)
else:
fields = ('hh', ..)
Any help is highly appreciated
You shouldn't do that and there's no way to achieve this. You can delete field on the fly in your __init__
function explicitly:
class ConstantVwModelForm(forms.ModelForm):
#couple attributes
def __init__(self, hasData, *args, **kwargs):
if hasData:
del self.fields['hh']
else:
del self.fields['xx']
del self.fields['yy']
class Meta:
model = ConstantVwModel
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments