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

ajax.py: get object by PK on form rendering #816

Closed
wants to merge 2 commits into from

Conversation

the-tosh
Copy link

@the-tosh the-tosh commented Sep 9, 2024

Does this workaround fix the issue described in PR #727?

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
@the-tosh
Copy link
Author

the-tosh commented Sep 9, 2024

If the solution is good enough, the linter recommendations will be added later

@aminalaee
Copy link
Owner

Thanks for the PR, Can you please explain in the issue/PR how you are solving this? I thought this was a new feature TBH, not sure what it is fixing.

@the-tosh
Copy link
Author

the-tosh commented Sep 9, 2024

When render a form, relationship field contains not an object, but string, which leads to the exception discussed in the aforementioned PR (sqlalchemy.exc.NoInspectionAvailable: No inspection system is available for object of type <class 'str'>)

I suggest to fetch an object from database in such cases (when a string is passed to loader.format() method). What I'm doubt of is all cases I cover with that solution or missing something

@aminalaee
Copy link
Owner

Can you open a new issue with a sample code?
I don't think when rendering a form this error should happen.

@the-tosh
Copy link
Author

the-tosh commented Sep 9, 2024

It would be literally the same as in PR #727:

...
File "/home/tosh/proj/src/venv/lib/python3.12/site-packages/sqladmin/templates/sqladmin/_macros.html", line 64, in template
    {{ field() }}
  File "/home/tosh/proj/src/venv/lib/python3.12/site-packages/wtforms/fields/core.py", line 176, in __call__
    return self.meta.render_field(self, kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/tosh/proj/src/venv/lib/python3.12/site-packages/wtforms/meta.py", line 64, in render_field
    return field.widget(field, **render_kw)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/tosh/proj/src/venv/lib/python3.12/site-packages/sqladmin/widgets.py", line 57, in __call__
    data = field.loader.format(field.data)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/tosh/proj/src/venv/lib/python3.12/site-packages/sqladmin/ajax.py", line 61, in format
    return {"id": str(get_object_identifier(model)), "text": str(model)}
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/tosh/proj/src/venv/lib/python3.12/site-packages/sqladmin/helpers.py", line 181, in get_object_identifier
    primary_keys = get_primary_keys(obj)
                   ^^^^^^^^^^^^^^^^^^^^^
  File "/home/tosh/proj/src/venv/lib/python3.12/site-packages/sqladmin/helpers.py", line 176, in get_primary_keys
    return tuple(inspect(model).mapper.primary_key)
                 ^^^^^^^^^^^^^^
  File "/home/tosh/proj/src/venv/lib/python3.12/site-packages/sqlalchemy/inspection.py", line 147, in inspect
    raise exc.NoInspectionAvailable(
sqlalchemy.exc.NoInspectionAvailable: No inspection system is available for object of type <class 'str'>

So, we could either teach the loader.format() method to work with string data (proposed in this PR) or rewrite field.data at some point of WTForms workflow, like this:

def process_formdata(self, valuelist: list) -> None:
        if valuelist:
            if self.allow_blank and valuelist[0] == "__None":
                self.data = None
            else:
                self._data = None
                value = valuelist[0]
                # here we go
                if isinstance(value , str):
                    self._formdata = self.loader.get_object(value)
               else:
                    self._formdata = value

@aminalaee aminalaee closed this Oct 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants