You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This seems related to a bunch of closed issues around handling nullable and non-nullable fields, but I recently was stumped by the following (IMO) weird behavior:
fromappimportadmin, db# vanilla Flask setupfromflask_admin.contrib.sqlaimportModelView# Declarative model using Flask-SQLAlchemy, Postgres as the backendclassDocument(db.Model):
id=db.Column(db.Integer, primary_key=True)
description=db.Column(db.String)
classDocument2(db.Model):
id=db.Column(db.Integer, primary_key=True)
description=db.Column(db.Text)
### Add to Flask-Admin instanceadmin.add_view(ModelView(Document, db.session))
Leads to Document instances having a null description field when they're created in Flask-Admin (what I'd expect by default) and Document2 instances having an empty string. In Postgres, I believe these are actually equivalent, so I can just use db.String as a workaround.
Is this a bug? Edge case of something about these column types I'm not thinking of (or that doesn;'t apply to Postgres)? Certainly a bit of a gotcha that I didn't jump out at me in any of the documentation or examples.
The text was updated successfully, but these errors were encountered:
The text converter doesn't do any work to check if the column is nullable and this is, I believe, the root of the issue.
I came up with the following to get around this
classMyModelConverter(sqla.form.AdminModelConverter):
real_text_columns= [
# any columns that should be treated like textarea fields should be listed here
]
@converts("Text")defconv_Text(self, field_args, **extra):
ifextra["column"] inself.real_text_columns:
returnsuper().conv_Text(field_args, **extra)
column=extra.pop("column")
returnself.conv_String(column, field_args, **extra)
There's certainly issues with this, like that you have to explicitly list out the "real" text columns. And I don't know what the implications of @converts("Text") are. However, it's working for my own purposes.
This seems related to a bunch of closed issues around handling nullable and non-nullable fields, but I recently was stumped by the following (IMO) weird behavior:
Leads to
Document
instances having a null description field when they're created in Flask-Admin (what I'd expect by default) andDocument2
instances having an empty string. In Postgres, I believe these are actually equivalent, so I can just usedb.String
as a workaround.Is this a bug? Edge case of something about these column types I'm not thinking of (or that doesn;'t apply to Postgres)? Certainly a bit of a gotcha that I didn't jump out at me in any of the documentation or examples.
The text was updated successfully, but these errors were encountered: