No non-NULL field required (allow blank lines) in FormAlchemy

I'm new to FormAlchemy and it seems like I'm not getting anything. I have a SQLAlchemy model defined as follows:

...
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)
...

Then, in my (Pylons) controller, I create a FormAlchemy form as follows:

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')])

The documentation says that "By default, NOT NULL columns are required. You can only add required columns, not delete them." But I want to allow non-null empty strings which are forbidden validators.required. Is there something like blank=True, null=FalseDjango?

To be more precise, I want a custom validator like the one below to allow empty lines with type=Noneor all values ​​that must be set to non-NULL and not empty :

# 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 , .

? .

+3
4

, (klugde, , , ).

  fs.serial_number.validators.remove(formalchemy.validators.required)
  fs.mac.validators.remove(formalchemy.validators.required)

( ) , FA , None, , , None ( validators.required, -coded). , : http://code.google.com/p/formalchemy/issues/detail?id=117

+4

, nullable=True ?

, . , .

, Django , NULL CharFields. NULL CharField, .

+3

, , , "", .

0

FormAlchemy None, null_as.

 whatever = Column(Unicode(999), required=False, null_as=("","\0"), nullable=False, default="", doc="Whatever for ever")

, null_as, None. ( - SELECT .)

, , .

0

Source: https://habr.com/ru/post/1718282/


All Articles