MongoEngine / mongoengine

A Python Object-Document-Mapper for working with MongoDB
http://mongoengine.org
MIT License
4.26k stars 1.23k forks source link

Add "blank" attribute for document fields. #786

Open mrTable opened 10 years ago

mrTable commented 10 years ago

Django model fields have two attributes "blank" and "null" that can be used for empty value validation, when mongoengine document field has only "required" attribute. Since mongoengine intends to have minimum amount of differences with django orm, it would be very helpful to have similar behavior about empty values.

We ran into a problem, that made me create this issue: There's a project, called mongodbforms, that allows to create django forms from mongoengine documents. ModelForm field in django becomes required only when blank==False, so it's possible to forbid field to have empty values on database level and don't make it required in the form. And, since mongoengine field has only one attribute related to the nullability of the field, mongodbforms has to set required attribute of formfield according to required attribute of mongoengine document. Best example is BooleanField. Once it's required, form cannot be submitted with unchecked checkbox of this BooleanField due to validation error. So it's impossible to use BooleanField in mongoengine document with required set to True in mongodbforms.

The proposal is to add "blank" attribute to mongoengine document field with the same behavior as "blank" attribute in django model fields.

DavidBord commented 10 years ago

null behaviour was merged in #734 , does it works for you?

mrTable commented 7 years ago

This behaviour looks odd comparing to django's.

Currently the required attribute is pretty much the same as the blank attribute of django's field. But the null attribute in django is a constraint to the database, whereas in mongoengine it's just a flag to whether use the default value or None.

In my opinion it should work exactly as the validation in django

This would also become an issue in StringField, when the user wants it to be required, but also wants to allow it to be an empty string.