Skip to content

Commit

Permalink
Merge pull request #1013 from pawl/test_fix_default_not_null
Browse files Browse the repository at this point in the history
SQLA: Don’t require fields that have a default or server_default value
  • Loading branch information
mrjoes committed Aug 23, 2015
2 parents f8e62a0 + 45f808f commit f774a37
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
7 changes: 6 additions & 1 deletion flask_admin/contrib/sqla/form.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,12 @@ def convert(self, model, mapper, prop, field_args, hidden_pk):

optional_types = getattr(self.view, 'form_optional_types', (Boolean,))

if not column.nullable and not isinstance(column.type, optional_types):
if (
not column.nullable
and not isinstance(column.type, optional_types)
and not column.default
and not column.server_default
):
kwargs['validators'].append(validators.InputRequired())

# Apply label and description if it isn't inline form field
Expand Down
24 changes: 23 additions & 1 deletion flask_admin/tests/sqla/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,22 @@ def __str__(self):

class Model2(db.Model):
def __init__(self, string_field=None, int_field=None, bool_field=None,
model1=None, float_field=None):
model1=None, float_field=None, string_field_default=None,
string_field_empty_default=None):
self.string_field = string_field
self.int_field = int_field
self.bool_field = bool_field
self.model1 = model1
self.float_field = float_field
self.string_field_default = string_field_default
self.string_field_empty_default = string_field_empty_default

id = db.Column(db.Integer, primary_key=True)
string_field = db.Column(db.String)
string_field_default = db.Column(db.Text, nullable=False,
default='')
string_field_empty_default = db.Column(db.Text, nullable=False,
default='')
int_field = db.Column(db.Integer)
bool_field = db.Column(db.Boolean)
enum_field = db.Column(db.Enum('model2_v1', 'model2_v2'), nullable=True)
Expand Down Expand Up @@ -1959,3 +1966,18 @@ class Model2(db.Model):

rv = client.get('/admin/model2/')
eq_(rv.status_code, 200)


def test_model_default():
app, db, admin = setup()
_, Model2 = create_models(db)

class ModelView(CustomModelView):
pass

view = ModelView(Model2, db.session)
admin.add_view(view)

client = app.test_client()
rv = client.post('/admin/model2/new/', data=dict())
assert_true(b'This field is required' not in rv.data)
2 changes: 1 addition & 1 deletion requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ flask-mongoengine
pillow
Babel<=1.3
flask-babelex
shapely
shapely==1.5.9
geoalchemy2
psycopg2
nose
Expand Down

0 comments on commit f774a37

Please sign in to comment.