ModelFormã®ãããªå é¨ã®Metaãè¦ãFormãèªåã§ä½ã
ModelFormã®ããã«ãå é¨ã®Metaãè¦ãã¯ã©ã¹ãèªåã§ä½ã£ã¦è¦ããã¨æã£ãã
æ¦è¦
djangoã§ã¯ã以ä¸ã®ããã«Metaã使ã£ã¦metaclassã«æ å ±ã渡ãã¦ãFormã¯ã©ã¹ãä½ããã
class ArticleForm(ModelForm): class Meta: model = Article
ã¾ãã以ä¸ã®ããã«fieldãç´æ¥æ¸ããã®ãdjangoãç¨æãã¦ããmetaclassã®æ©è½*1ã
class ContactForm(forms.Form): topic = forms.ChoiceField(choices=TOPIC_CHOICES) message = forms.CharField(widget=forms.Textarea()) sender = forms.EmailField(required=False)
åãããã«Metaãã¿ãã¯ã©ã¹ãä½ã£ã¦ã¿ããã¨æã£ãã
ä»åä½ãæ©è½
ãã©ã¼ã ã®ä»¥ä¸ã®æ©è½ãåãã¦ã¿ãã
- 人éãè¦ããããããªhtmlã®ã¬ã¤ã¢ã¦ããããæ©è½
- å ¥å(POST)ããããã¼ã¿ãæ¤è¨¼ããæ©è½
ãããããLayoutãã©ã¼ã ãããValidateãã©ã¼ã ãã¨å¼ã¶ãã¨ã«ããã
ãã®æã®Validateãã©ã¼ã ãä½ã£ã¦ã¿ããã¨ã«ããã
注æ
å
¥åå¤ã®æ¤è¨¼ã«å¤±æããæãåå
¥åãä¿ãããã
ãªã®ã§ãValidateãã©ã¼ã ããLayoutãã©ã¼ã ã¸å¤æã§ããå¿
è¦ãããã
使ãæ¹
以ä¸ã®ããã«ãã¦ä½¿ãã
formã®å®ç¾©
class LayoutForm(forms.Form): ## ã©ããã風ã«è¡¨ç¤ºããããªã©ããããæ¸ãã item = forms.ChoiceField(choices=[(i, i) for i in xrange(3)]) date = forms.DateTimeField(required=False) class ValidateForm(ValidateForm): ## å¤ã®æ¤è¨¼æ¹æ³ãããããæ¸ã item = forms.IntegerField() date = forms.DateTimeField() class Meta(object): layout_form = LayoutForm #form.layout_form()ãå¼ã¹ãããã«ãªã.
å®ç¾©ãããã¨ãã¦ããFormã¯ã©ã¹ã大ããããªãéãç ©éã«ãªãã ãã§ãã¾ãæå³ããªããã ããã©ã
viewã§ã®å©ç¨
#... form = ValidateForm(dict(item=3), prefix="foo") if not form.is_valid(): lform = form.layout_form() #formã§çæãããerror.listãæã£ã¦ãã #...
ValidateFormãè¡ã£ã¦ããä½æ¥
- form.layout_form()ã®çæ
- layoutãã©ã¼ã ã¨Validateãã©ã¼ã ã®ãã£ã¼ã«ãã®å·®åãæ¤è¨¼
- Validateãã©ã¼ã ã§å®ç¾©ãããå ¨ã¦ã®ãã£ã¼ã«ããLayoutãã©ã¼ã ãæã£ã¦ããªãã¨ã¨ã©ã¼
ä½ãæ¹
- Metaã«å«ã¾ããæ å ±ãæ ¼ç´ããValidateFormOptionsã¨ããã¯ã©ã¹ãä½ã
- ValidateFormMetaclassã¨ããmetaclassãä½ã
- __metaclass__ã«å®ç¾©ããmetaclassã渡ããã¯ã©ã¹ãä½ã
import django.forms.forms as forms class ValidateFormOptions(object): def __init__(self, options=None): self.layout_form = getattr(options, "layout_form", None) self.translate = getattr(options, "translate", None) def layout_form_function_fuctory(formclass): def layout_form(self): form = formclass(auto_id = self.auto_id, prefix = self.prefix, error_class = self.error_class, label_suffix = self.label_suffix, empty_permitted = self.empty_permitted) form._changed_data = self._changed_data form.data = self.data form._errors = self._errors return form return layout_form class ValidateFormMetaclass(forms.DeclarativeFieldsMetaclass): def __new__(cls, name, bases, attrs): new_class = super(ValidateFormMetaclass, cls).__new__(cls, name, bases, attrs) opts = new_class._meta = ValidateFormOptions(getattr(new_class, "Meta", None)) if opts.layout_form: ## validate layout_form_fields = opts.layout_form.base_fields for k in new_class.base_fields.keys(): if layout_form_fields.get(k) is None: raise AssertionError("%s is not found in %s class" % (k, opts.layout_form.__name__)) if opts.translate: new_class.layout_form = opts.translate else: new_class.layout_form = layout_form_function_fuctory(opts.layout_form) class ValidateForm(forms.BaseForm): __metaclass__ = ValidateFormMetaclass