Search code examples
mongodbflaskmongoengineflask-adminflask-mongoengine

Cannot remove last field from input in panel


In Flask panel I have class which inherits embedded document and I have inside

persons = db.ListField(db.ReferenceField('Person', required=False, null=True), default=[], required=False)

When I want to remove last Person from my input field via Flask Admin Panel it saves document but do not remove, when I have two I can remove one, but cannot stay empty when I try to remove second. I tried different combinations with flags and I put breakpoints in pre_save and post_save but in both cases when I remove last Person it shows that person is still inside. How to remove this constraint from Form ?


Solution

  • It seems that nothing happens on flask-admin side when you submit a form with an empty list. So I did a trick using on_model_change method, it would look like this in your case :

      def on_model_change(self, form, model, is_created):         
        if not 'persons' in request.form :                        
            model.persons = []