iommirocks / iommi

Your first pick for a django power cord

Home Page:http://iommi.rocks

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

choice_queryset in filter forms should simply filter by pk

berycz opened this issue · comments

commented

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 into Found more than one object for name "{text}"
plus it creates some unnecessary db queries

E.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 the AlbumTable 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 no where in the sql query) and after page-reload there is an error message Found more than one object for name "Damnation"

that is fixable with

register_search_fields(model=Artist, search_fields=["name", "country"], allow_non_unique=True)

but when you switch the search fields, then it throws the error too

register_search_fields(model=Artist, search_fields=["country", "name"], allow_non_unique=True)

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