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

Automatically search through ForeignKey fields? #20

Open
kevinrenskers opened this issue Mar 1, 2024 · 1 comment
Open

Automatically search through ForeignKey fields? #20

kevinrenskers opened this issue Mar 1, 2024 · 1 comment

Comments

@kevinrenskers
Copy link

I have many models that have a ForeignKey field to the User model. When I use django-admin-site-search to search for "someuser@example.com" by default I only get that user as a result, not also all the objects that belong to this user.

I can extend the filter like so:

class MyAdminSite(AdminSiteSearchView, admin.AdminSite):
    site_header = "Sound Radix site admin"

    def filter_field(self, query, field):
        if isinstance(field, ForeignKey) and field.related_model is User:
            return Q(**{f"{field.name}__email__icontains": query})

        return super().filter_field(query, field)

And then I see many more results, as I'd expect. But it's kind of annoying to have to add these filters by hand for all possible related models (obviously I have ForeignKey fields to other models too). Can this be automated somehow?

@ahmedaljawahiry
Copy link
Owner

Yeah - I think having this as an opt-in via the admin's search fields, as per #22, is the way to go. Adding it on top of the current implementation would make performance even more problematic.

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

No branches or pull requests

2 participants