Dynamically Displaying Fields in a ModelForm
The Problem : I want to dynamically include some fields in a ModelForm based on some external criteria. Sometimes I want the fields displayed, sometimes I don't.
I'm going to try to explain this scenario through a (albeit contrived) example. I have a Model that looks like the following:class Suff(models.Model):
foo = models.CharField(max_length=255)
bar = models.BooleanField(default=False, blank=True)
''' is this model's foo attribute set to 'bar' '''
return self.foo == 'bar'
Normally, if I needed a Form for this Model, I would subclass a ModelForm like the following:class StuffForm(models.ModelForm):
model = Stuff
fields = ('foo', 'bar')
However, if I do NOT want the 'bar' field to be displayed by default I would need to remove it from the ModelForms list of fields (or use something like exclude = ('bar', ) ). But, if this form is created with an instance of Stuff whose foo attribute contains the string bar , I would like for the Form's 'bar' field to be displayed.
I originally tried to accomplish this task by overridding StuffForm's __init__ method, and adding a new BooleanField when the desired circumstances arose... However, I stumpled across Ross Poulton's Dynamic ModelForms in Django , and then I realized it would be much easier to prevent a ModelForm's Field from being displayed than it would be to dynamically create one.
In order to accomplish this, the StuffForms's __init__ method would look something like the following:def __init__(self, *args, **kwargs):
super(StuffForm, self).__init__(*args, **kwargs)
# If this form is created without an instance, OR
# If the instance's foo field is != 'bar'
if not kwargs.has_key('instance') or (kwargs.has_key('instance') and \
# Remove this field from the form.
Done. I get all the benefits of a ModelForm, and the bar field is not displayed unless it should be.
本文开发（python）相关术语:python基础教程 python多线程 web开发工程师 软件开发工程师 软件开发流程