Closed berycz closed 9 months ago
I think I'm having another issue with this, simply filters on FK don't work
in MyModel
I have 2 FK's to user model (client
, employee
), in the filter form I have filters for both as select2
in apps.py
I have:
register_search_fields(
model=get_user_model(),
search_fields=[
"first_name", "last_name", "email", "company_name", "city", "street",
"company_id_number", "tax_id_number"
],
allow_non_unique=True
)
and when I check the ?client=123&_iommi_sql_trace
, it does
SELECT "users_user"."id", "users_user"."tax_id_number" FROM "users_user" WHERE "users_user"."id" = 144 LIMIT 21
and then 8 queries to User model for each of the search_fields
with WHERE "users_user"."{search_fields item}" = '1234567890'
where the 1234567890
is the tax_id_number
of user.id=123
and then some more sql queries and then it returns ALL MyModel
objects in the table
we talked about this with @boxed on discord (2023-08-23)
choice_queryset
in filter forms doesn't simply filter by pk, but translates it to the display text, then searches for an object with that text and gets the pk but that can sometimes run intoFound more than one object for name "{text}"
plus it creates some unnecessary db queriesE.g. in
Artist
model having multiple bands with the same name (e.g. "Damnation", there seem to be too many of metal bands with that name) so in theAlbumTable
you make filter with chocies{name} ({country})
but then when you select one of them you get table of all albums from all artists (there is nowhere
in the sql query) and after page-reload there is an error messageFound more than one object for name "Damnation"
that is fixable with
but when you switch the search fields, then it throws the error too