Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mongoengine Support in Version 2 #2541

Open
Bastian-Kuhn opened this issue Oct 16, 2024 · 9 comments
Open

Mongoengine Support in Version 2 #2541

Bastian-Kuhn opened this issue Oct 16, 2024 · 9 comments

Comments

@Bastian-Kuhn
Copy link
Contributor

Hello team,

First of, thank you very much for flask-admin.
As already mentioned, in this ticket, my tool heavily depends on mongoengine.

After some first proof of concepts, I found that migration to pymongo would be a huge step backwards. It's for my program, which basically consists of DB Models like a rewrite with fewer features and comfort after. (Example Filters also missing)

As a replacement for flask-mongoengine I found flask-mongoeine-3 which works well, and can replace the old one without any changes of code.

for flask-admin I was able with just some lines of code to make the contrib/mongoengine work again.
And my Plan B would to just ship that part with my Project. But since I'm sure others could use it too,
I want to ask why It can't stay as part of the flask-admin? I need to fix it anyway for me to keep it working, so I could also help with Pull Requests. I just would need to change everything in order that the tests work again. But the function is already there by changing two imports.

Best
Bastian

@samuelhwilliams
Copy link
Contributor

samuelhwilliams commented Oct 26, 2024

Thanks for the update. Disappointing to hear that pymongo doesn't look like a feasible replacement.

flask-mongoengine-3 is broadly an option. My initial concern is that it appears to quite inactive as far as maintenance goes, and doesn't seem to have a broad base of support (in terms of maintainers).

I'm not really against it per se, but bear in mind I have no personal motivation either as I don't use mongo in my day-to-day so it's not work I'd be heavily invested in.

Flask-Admin is a community maintained project so I think it would be very possible for you to look at adding support for flask-mongoengine-3 back (which hopefully may not be much harder than reverting the PR which took it away and making whatever tweaks/changes are needed for -3 support).

Maybe join the Pallets discord and check out the #pallets-eco channel? I'm happy to look at PRs you put up adding support (back) for it, as and when I can

@karpitsky
Copy link
Contributor

Hello,

My Flask app uses pure MongoEngine and Flask-Admin, and it works perfectly, thanks! Maybe there's an option to remove the Flask-MongoEngine dependency but keep MongoEngine.

As far as I can see, the current version of Flask-Admin uses only the orm and fields functionality from Flask-MongoEngine in the form.py file, but everything else is from MongoEngine.

Additionally, the orm module is deprecated in Flask-MongoEngine.

@Bastian-Kuhn
Copy link
Contributor Author

As far as I can see, the current version of Flask-Admin uses only the orm and fields functionality from Flask-MongoEngine in the form.py file, but everything else is from MongoEngine.

But isn’t that a crucial part of the code, given its responsibility for the WTF Forms? Otherwise, I could start exploring alternatives, possibly by shifting the related logic from flask_mongoengine into the flask_admin module.

@karpitsky
Copy link
Contributor

I've just tried to replace Flask-MongoEngine imports with MongoEngine and WTForms and it's working fine for me.

Here's the diff.

To be honest, I haven't tested all the field types yet.

@Bastian-Kuhn
Copy link
Contributor Author

Good idea to remove the requirement completely, will test that. It just will take me some days to start, much to do currently. Thank you very much already.

@samuelhwilliams
Copy link
Contributor

I've just tried to replace Flask-MongoEngine imports with MongoEngine and WTForms and it's working fine for me.

If this is workable, it sounds like a very positive idea and alleviates concerns around maintenance issues with flask-mongoengine and its forks 👍

@Bastian-Kuhn
Copy link
Contributor Author

@karpitsky I just tested your fixed version of forms.py and It works fine. Perfect

@jcampbell05
Copy link

jcampbell05 commented Nov 26, 2024

is there a way of extracting the contrib package into it's own package so we could refactor it now to need mongoengine ?

@Bastian-Kuhn
Copy link
Contributor Author

Ok, found one Problem. ReferenceFields stopped working.
If nested in a List, they just can't find a matching entry.

But when used like name = db.ReferenceField(),
there is a View Error in FlaskAdmin:

Traceback (most recent call last):
  File "/usr/local/lib/python3.12/site-packages/flask_admin/base.py", line 369, in _run_view
    return fn(self, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/flask_admin/model/base.py", line 2093, in create_view
    form = self.create_form()
           ^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/flask_admin/model/base.py", line 1332, in create_form
    return self._create_form_class(get_form_data(), obj=obj)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/wtforms/form.py", line 209, in __call__
    return type.__call__(cls, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/flask_admin/form/__init__.py", line 22, in __init__
    super(BaseForm, self).__init__(formdata=formdata, obj=obj, prefix=prefix, **kwargs)
  File "/usr/local/lib/python3.12/site-packages/wtforms/form.py", line 281, in __init__
    super().__init__(self._unbound_fields, meta=meta_obj, prefix=prefix)
  File "/usr/local/lib/python3.12/site-packages/wtforms/form.py", line 49, in __init__
    field = meta.bind_field(self, unbound_field, options)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/wtforms/meta.py", line 28, in bind_field
    return unbound_field.bind(form=form, **options)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/wtforms/fields/core.py", line 387, in bind
    return self.field_class(*self.args, **kw)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/wtforms/fields/choices.py", line 85, in __init__
    super().__init__(label, validators, **kwargs)
  File "/usr/local/lib/python3.12/site-packages/wtforms/fields/choices.py", line 25, in __init__
    super().__init__(label, validators, **kwargs)
TypeError: Field.__init__() got an unexpected keyword argument 'allow_blank'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.12/site-packages/flask/app.py", line 1498, in __call__
    return self.wsgi_app(environ, start_response)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/flask/app.py", line 1476, in wsgi_app
    response = self.handle_exception(e)
               ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/flask_restx/api.py", line 672, in error_router
    return original_handler(e)
           ^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/flask/app.py", line 1473, in wsgi_app
    response = self.full_dispatch_request()
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/flask/app.py", line 882, in full_dispatch_request
    rv = self.handle_user_exception(e)
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/flask_restx/api.py", line 672, in error_router
    return original_handler(e)
           ^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/flask/app.py", line 880, in full_dispatch_request
    rv = self.dispatch_request()
         ^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/flask/app.py", line 865, in dispatch_request
    return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args)  # type: ignore[no-any-return]
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/flask_admin/base.py", line 69, in inner
    return self._run_view(f, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/flask_admin/base.py", line 371, in _run_view
    return fn(cls=self, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^
TypeError: BaseModelView.create_view() got an unexpected keyword argument 'cls'

I made out:

TypeError: Field.__init__() got an unexpected keyword argument 'allow_blank'

so i Changed:

    def conv_Reference(self, model, field, kwargs):
        #kwargs['allow_blank'] = not field.required

Which fixed the error, but still no entries show up.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

4 participants