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: searching by id #522

Open
Ilya-Green opened this issue Mar 7, 2024 · 2 comments
Open

Bug: searching by id #522

Ilya-Green opened this issue Mar 7, 2024 · 2 comments
Labels
bug Something isn't working

Comments

@Ilya-Green
Copy link

Describe the bug
Is there ant way to enable searching by id?
If i trying to include "id" in "searchable_fields" search starts working incorrect: it stops filtering reccord, but only highlights matches on the current page if they are if or not.

without "id" in "searchable_fields":
image
image

with "id" in "searchable_fields":
image
image

To Reproduce

class NotesView(MyModelView):
    fields = [
        Note.id,
        Note.content,
    ]

    searchable_fields = [
        Note.id
    ]

Environment (please complete the following information):

  • ORM/ODMs: SQLModel
@Ilya-Green Ilya-Green added the bug Something isn't working label Mar 7, 2024
@sglebs
Copy link

sglebs commented Mar 12, 2024

I have faced the same problem and have the same need to search by an ID (primary key) or the relationship itself. In my case:

class User(Base):
    __tablename__ = 'users'

    id = Column(Text, primary_key=True, default=cuid)
    created_at = Column(TIMESTAMP(timezone=True, precision=3), nullable=False, server_default=text("CURRENT_TIMESTAMP"))
    updated_at = Column(TIMESTAMP(timezone=True, precision=3), nullable=False,  onupdate=current_date_time_with_tz, default=current_date_time_with_tz)
    client_id = Column(ForeignKey('clients.id', ondelete='SET NULL', onupdate='CASCADE'))
    role = Column(Enum(UserRole, name='UserRole'), server_default=text("'REGULAR'::\"UserRole\""))
    name = Column(Text)
    email = Column(Text, nullable=False, unique=True)

    client = relationship('Client')

A User belongs to a Client. I want to search based on the Client (or the Client id if I am forced to, as plan B)

@Ilya-Green
Copy link
Author

Ilya-Green commented Mar 15, 2024

Possible solution (for sqla):
startlette_admin/contrib/sqla/view.py:

    def get_search_query(self, request: Request, term: str) -> Any:
        clauses = []
        for field in self.fields:
            if field.searchable and type(field) in [
                StringField,
                TextAreaField,
                EmailField,
                URLField,
                PhoneField,
                ColorField,
                # there can be added custom fieldsto make them searchable
            ]:
                attr = getattr(self.model, field.name)
                clauses.append(cast(attr, String).ilike(f"%{term}%"))
+       id_clause = cast(self.model.id, String).ilike(f"%{term}%")
+       clauses.append(id_clause)
        return or_(*clauses)

I must warn you that the code on the basis of which I am making changes is old and may differ from the latest version of starlette_admin, but I marked the lines that I added with a plus sign so that the principle would be clear

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