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

Bug: Sorting custom fields descendingly gives error #511

Open
omarmoo5 opened this issue Feb 18, 2024 · 4 comments
Open

Bug: Sorting custom fields descendingly gives error #511

omarmoo5 opened this issue Feb 18, 2024 · 4 comments
Labels
bug Something isn't working

Comments

@omarmoo5
Copy link
Contributor

Describe the bug
Whenever i try to sort a custom field descendingly it gives an error.

To Reproduce
For example here in the demo example , the comments field of users page.

If you tried to sort the comments counter field descendingly . it fails.

@omarmoo5 omarmoo5 added the bug Something isn't working label Feb 18, 2024
@Ilya-Green
Copy link

Ilya-Green commented Mar 6, 2024

which one is comment field? can you provide screen?
if you mean relation field it bacuse it can't undarstand how to sort it, by which parametr.
One of possible solution is may be editing helpers.py: def build_order_clauses: adding this to func:
if attr_key == 'status': attr_key = 'status_id'
but i suppose it will work only for 'hasone' relations.

@Ilya-Green
Copy link

Ilya-Green commented Mar 6, 2024

also i found this construction in demo source code, it can be solution:

class PostView(ModelView):
    fields = [
        "id",
        "publisher",
    ]
    sortable_field_mapping = {"publisher": User.full_name}

@Ilya-Green
Copy link

this is code of how this counter works:
class CommentCounterField(StringField):
async def parse_obj(self, request: Request, obj: Any) -> Any:
assert isinstance(obj, User)
return len(obj.comments)

it just counts the number of comments and returns a number, but it seems it is not set by which parameter such a field should be sorted

@omarmoo5
Copy link
Contributor Author

omarmoo5 commented Mar 6, 2024

this is code of how this counter works:

class CommentCounterField(StringField):

async def parse_obj(self, request: Request, obj: Any) -> Any:

    assert isinstance(obj, User)

    return len(obj.comments)

it just counts the number of comments and returns a number, but it seems it is not set by which parameter such a field should be sorted

Thanks for your reply, Surprisingly it can sorts them ascendingly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants