フォーマルチェミーで非ヌルフィールド(空の文字列を許可する)を必要としないでください
-
12-09-2019 - |
質問
私はフォーマルチェミーにかなり初心者であり、何かが得られないようです。私はこのように定義されたsqlalchemyモデルを持っています:
...
class Device(meta.Base):
__tablename__ = 'devices'
id = sa.Column('id_device', sa.types.Integer, primary_key=True)
serial_number = sa.Column('sn', sa.types.Unicode(length=20), nullable=False)
mac = sa.Column('mac', sa.types.Unicode(length=12), nullable=False)
ipv4 = sa.Column('ip', sa.types.Unicode(length=15), nullable=False)
type_id = sa.Column('type_id', sa.types.Integer,
sa.schema.ForeignKey('device_types.id'))
type = orm.relation(DeviceType, primaryjoin=type_id == DeviceType.id)
...
それから、私の(パイロン)コントローラーで、私は次のようなフォーマルチェムフォームを作成します。
c.device = model.meta.Session.query(model.Device).get(device_id)
fs = FieldSet(c.device, data=request.POST or None)
fs.configure(options=[fs.ipv4.label(u'IP').readonly(),
fs.type.label(u'Type').with_null_as((u'—', '')),
fs.serial_number.label(u'S/N'),
fs.mac.label(u'MAC')])
ドキュメントには、「デフォルトでは、ヌル列が必要ではありません。必要なものを追加することしかできず、削除しないでください。」と書かれています。 validators.required
禁止。何かがありますか blank=True, null=False
ダジャンゴで?
より正確には、空の文字列を許可するように、以下のようなカスタムバリデーターが必要です type=None
または、非ヌルおよび非空白に設定されるすべての値:
# For use on fs.mac and fs.serial_number.
# I haven't tested this code yet.
def required_when_type_is_set(value, field):
type_is_set = field.parent.type.value is not None:
if value is None or (type_is_set and value.strip() = ''):
raise validators.ValidationError(u'Please enter a value')
可能であれば、サルパッチを控えたいと思います formalchemy.validators.required
または他のKludges。設定したくない nullable=True
モデルフィールドでは、それも適切な解決策ではないようです。
そのような場合、フォームを検証する正しい方法は何ですか?事前に提案をありがとう。
解決
Finally found a (klugde, but seems this is the only sane choice) way to do this.
fs.serial_number.validators.remove(formalchemy.validators.required)
fs.mac.validators.remove(formalchemy.validators.required)
(Regarding my validator) Note, that FA will completely skip all validation when the value is None
, because, by convention, it won't pass None
to validators (except for validators.required
, which is hard-coded). I've filed an enhancement request ticket trying to solve this: http://code.google.com/p/formalchemy/issues/detail?id=117
他のヒント
Can you explain why nullable=True
would not be the solution?
To me it seems useless to store empty strings in the database and I would not encourage it. If there is no data than choose the more efficient type for the database.
Personally I think the Django solution in case of strings is wrong since it does not really support NULL in CharFields. If you want to store NULL in a CharField you will have to do it manually in the code.
This is exactly the same issue i'm having, where I often write interfaces for existing databases and want to use formalchemy but end up having to manually remove 'requiredness' as I am not able to change the database.
You can teach FormAlchemy to not replace an empty input with None by changing the null_as
column attribute.
whatever = Column(Unicode(999), required=False, null_as=("","\0"), nullable=False, default="", doc="Whatever for ever")
Formalchemy will replace input that's equal to the second member of the null_as
tuple with None
. (The first is the display text for SELECTs and similar fields.)
If you set that to a string the user cannot input, this will never happen.