SmileyChris / django-countries

A Django application that provides country choices for use with forms, flag icons static files, and a country field for models.

Home Page:https://pypi.python.org/pypi/django-countries

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Queryset field lookups for country code no longer works as of 7.3

chenyaoy opened this issue · comments

The change in 7.3, Make full English country names work in database lookups, for example, Person.objects.filter(country__icontains="zealand") has caused queryset filter field lookups for the two character country code to no longer work.

For example, queryset.filter(country__regex=rf"^GB$") would return results from my DB in 7.2.1, but in 7.3 and later it returns an empty queryset. I also tested that queryset.filter(country__contains="GB") also incorrectly returns an empty queryset. However, queryset.filter(country="GB") works as expected and so does queryset.filter(country__contains="Kingdom")

Yep, it's definitely an incompatible change (I really should have called it 8.0 I feel). It only changes the behaviour of advanced filters, because they don't really make a lot of sense against country codes - e.g. your example of __contains has little practical benefits. Even __regex I'd argue has limited benefits.

However it doesn't actually look difficult to make this work for the country code as well.
Maybe

return cast(CountryField, self.lhs.output_field).countries.by_name(
value, regex=True, insensitive=self.insensitive
)
could just be appended with the field in an F()? That'd make it work for all the lookups and seems like a generally good improvement.

I'll leave this open so someone, if not me, will look closer at it some time. If you're really keen on this feature, feel free to try to implement that!