marshmallow-formでmetadataのoverrideが出来るようになりました

marshmallow-formでmetadataのoverrideが出来るようになりました。

marshmallow-formでmetadataのoverrideが出来るようになりました。 他にも幾つか変更点がありますがコレが大きな変更です。

override出来るのは以下の2箇所です

  • Nestedで作ったフィールドの子フィールドのmetadata
  • 継承したフォームのフィールドのmetadata

Nestedで作ったフィールドの子フィールドのmetadata

Nestedで作ったフィールドの子フィールドのmetadataをoverrideするには、overridesオプションを使います。

import marshmallow_form as mf


class DateTriple(mf.Form):
    year = mf.Int(doc="year of date", widget="int")
    month = mf.Int(doc="month of date", widget="int")
    day = mf.Int(doc="day of date", widget="int")

    def make_object(self, data):
        from datetime import date
        return date(data["year"], data["month"], data["day"])


class Form(mf.Form):
    class Meta:
        metadata = {"action": "POST"}

    ctime = mf.Nested(DateTriple, overrides={"year": {"doc": "year of ctime"}})

form = Form()
print(form["action"])  # POST
print(form.ctime.year["doc"])  # year of ctime
print(form.ctime.year["month"])  # year of date

継承したフォームのフィールドのmetadata

継承したフォームのフィールドのmetadataをoverridesするには、クラスのMeta部分にoverridesを書きます。

class HasName(mf.Form):
    name = mf.String(doc="name of hasname")
    ctime = mf.Nested(DateTriple)


class Form(HasName):
    class Meta:
        overrides = {
            "name": {"doc": "name of form"},
            "ctime": {"year": {"doc": "year of date of form"}}
        }

form = Form()
print(form.name["doc"])  # name of form
print(form.ctime.year["doc"])  # year of date of form

nestedなfieldのoverrideも同様にもう一段ネストした辞書を書くだけです。