django-countries icon indicating copy to clipboard operation
django-countries copied to clipboard

Support Django 5.0

Open adamchainz opened this issue 2 years ago • 10 comments

Builds on #424, which should be merged first. Add support for Django 5.0.

adamchainz avatar Oct 13 '23 22:10 adamchainz

Currently there are three test failures:

FAILED django_countries/tests/test_admin_filters.py::TestCountryFilter::test_choices - AssertionError: False != True
FAILED django_countries/tests/test_fields.py::TestModelForm::test_translated_choices - AssertionError: 'Afghanistan' != 'Afganio'
FAILED django_countries/tests/test_widgets.py::TestCountrySelectWidget::test_render - AssertionError: 0 != 1 : Found 0 instances of '<option value="AU">

The latter two cases are caused by Django’s changes to handling choices. It seems that part of this change means that Django iterates over the Countries class at field initialization, rather than lazily when required. This means that the translations are done once rather than on-demand.

One option is to instead use a no-argument callable, which will always be called later. I tried adding __call__ to Countries but that didn’t work, I suspect a wrapping function will be required.

adamchainz avatar Oct 13 '23 22:10 adamchainz

I think we can stop using LazyTypedChoiceField / LazyTypedMultipleChoiceField and all related classes on Django 5.0, since a callable for choices is treated lazily by Django. But unfortunately there’s a bug with that laziness, which I’ve reported upstream: https://code.djangoproject.com/ticket/34899 .

adamchainz avatar Oct 14 '23 10:10 adamchainz

The fix ( https://github.com/django/django/pull/17370 ) was merged and released in Django 5.0b1. I have updated the code to use the new callable choices support and made the Lazy* classes no-ops on Django 5.0+.

I also added a fix for the admin change:

The django.contrib.admin.AllValuesFieldListFilter, ChoicesFieldListFilter, RelatedFieldListFilter, and RelatedOnlyFieldListFilter admin filters now handle multi-valued query parameters.

Everything is passing now.

adamchainz avatar Nov 08 '23 14:11 adamchainz

@SmileyChris can this please be merged and released? It is blocking upgrade to Django 5.0 (I am testing RC1 now). Thank you.

washeck avatar Nov 28 '23 10:11 washeck

You can install my commit from the Git repo with this specification for Pip:

django-countries @ git+https://github.com/SmileyChris/django-countries@58f258402072a18756a15daa81325428382bd946

(or with the appropriate syntax for Poetry etc.)

That’s what I’ve done for testing my client’s project on Django 5.0 RC1.

adamchainz avatar Nov 28 '23 10:11 adamchainz

Yes, that's what I'm doing (django_5.0 branch from your fork) and it's working so I think it would be good to issue the release before Django 5.0 is released and people start upgrading. Thanks you for the PR.

washeck avatar Nov 28 '23 10:11 washeck

I did install from pip using the above command, but I still have the below issue in django 5. Am I missing something ? @adamchainz would you have any idea ? It seems we are quite a few to have this issue.

\django_countries\fields.py", line 22, in <module>
    _entry_points = importlib.metadata.entry_points().get(
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'EntryPoints' object has no attribute 'get'

kribot avatar Dec 16 '23 06:12 kribot

Any new on when this is going to be released? Django 5 it out for a while and after I tried to upgrade, my admin pages got broken. Here is a corresponding Django ticket:

https://code.djangoproject.com/ticket/35046

zyv avatar Dec 25 '23 17:12 zyv

Any chance this could be released? It's blocking our upgrade to Django 5.0. Thanks.

KlemenS189 avatar Jan 08 '24 11:01 KlemenS189

I did install from pip using the above command, but I still have the below issue in django 5. Am I missing something ? @adamchainz would you have any idea ? It seems we are quite a few to have this issue.

\django_countries\fields.py", line 22, in <module>
    _entry_points = importlib.metadata.entry_points().get(
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'EntryPoints' object has no attribute 'get'

It looks like @adamchainz' code works with Python < 3.10 perfectly. However, the code doesn't work with Python 3.12.

If I understand correctly, the importlib.metadata API has been changed since Python 3.10.

Other projects had the same issue, e.g. https://github.com/ewels/rich-click/issues/140.

I've modified Adam's code to make it work with Python 3.10 and above: https://github.com/JulianTenschert/django-countries/commit/f085ab1eb38dc5aece08092a03b083d00622cf94. This works well.

JulianTenschert avatar Jan 08 '24 18:01 JulianTenschert

@SmileyChris Whats needed to move forward with this? How can we help?

Thanks

andriijas avatar Mar 13 '24 10:03 andriijas

I just squashed @JulianTenschert ’s commit into mine to make merging or installing from git easier.

adamchainz avatar Mar 16 '24 22:03 adamchainz

Thanks for your efforts, however I think the current situation is not sustainable in the longer term. If anyone has direct contacts with Chris personally or someone else at Lincoln Loop, it would be very nice to push a bit so as to unblock this package and put it in a co-maintainership situation (by Jazzband adoption or any other mean).

claudep avatar Mar 23 '24 13:03 claudep

Hey everyone, I managed to get in contact with SmileyChris and he gave me contributor access to the repo. I'm currently trying to familiarize myself with the state of the project. @adamchainz Thank you for creating this PR and keeping it alive. What is the current state of the PR? Does it still rely on the Django 4.2 PR or have they been combined in the meantime? Does it also address the python 3.12 fixes?

From my side, this PR takes priority and after I will look into updating the test matrix to test all currently supported django and python combination instead of only the latest.

MarcoGlauser avatar Mar 25 '24 05:03 MarcoGlauser

I did not combine the Django 4.2 PR into this one, but I did merge in the Python 3.12 fix. Thank you for taking over the project!

adamchainz avatar Mar 25 '24 06:03 adamchainz

Sorry I've been so absent! I finally got things polished up -- I had to fix transifex not pulling in changes due to their api changes, and some tox updates for 4 but it's done, and I pushed 7.6!

SmileyChris avatar Mar 27 '24 03:03 SmileyChris

One small follow-up fix is needed: https://github.com/SmileyChris/django-countries/pull/460

Without this check even a trivial use of CountryFilter will raise an exception:

  File "/usr/local/lib/python3.12/site-packages/django_countries/filters.py", line 27, in choices
    selected = force_str(lookup) in value
               ^^^^^^^^^^^^^^^^^^^^^^^^^^

Exception Type: TypeError at /admin/country_test/person/
Exception Value: argument of type 'NoneType' is not iterable

foutrelis avatar Apr 01 '24 11:04 foutrelis