我们从Python开源项目中,提取了以下3个代码示例,用于说明如何使用django.forms.models.ModelForm()。
def get_form(self, request, obj=None, **kwargs): form = super(_BaseAdmin, self).get_form(request, obj, **kwargs) def clean(form): super(ModelForm, form).clean() self.clean_form(request, form) form.clean = clean return form
def get_model(self): if self.model: return self.model if issubclass(self.form, ModelForm): model = self.form._meta.model if model: return model raise ImproperlyConfigured(u"Please set entry 'model' attribute or use " u"ModelForm subclass as a form")
def setUpClass(cls): super(WizardTestMixin, cls).setUpClass() # This prevents auto-discovery, which would otherwise occur as soon as # tests start, creating unexpected starting conditions. wizard_pool._discovered = True class PageWizard(Wizard): pass # This is a basic Wizard cls.page_wizard = PageWizard( title=_(u"Page"), weight=100, form=WizardForm, model=Page, template_name='my_template.html', # This doesn't exist anywhere ) class SettingsWizard(Wizard): pass # This is a Wizard that uses a ModelForm to define the model cls.user_settings_wizard = SettingsWizard( title=_(u"UserSettings"), weight=200, form=ModelWizardForm, ) class TitleWizard(Wizard): pass # This is a bad wizard definition as it neither defines a model, nor # uses a ModelForm that has model defined in Meta cls.title_wizard = TitleWizard( title=_(u"Page"), weight=100, form=BadModelForm, template_name='my_template.html', # This doesn't exist anywhere )