-
Notifications
You must be signed in to change notification settings - Fork 44
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
Support Django 5.0 and Python 3.12 #74
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,12 @@ | |
|
||
Backward-incompatible changes for released versions are listed here (for 0.5 onwards.) | ||
|
||
* Added support for Django 4.2. | ||
* Added support for Django 4.2 and 5.0. | ||
|
||
* Added support for Python 3.12. | ||
|
||
* Dropped the `VERSION` and `__version__` attributes. To check the version of the package, use `importlib.metadata.version("django-typed-models")` ([docs](https://docs.python.org/3/library/importlib.metadata.html#distribution-versions) / | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
[backport](https://pypi.org/project/importlib-metadata/)). | ||
|
||
## 0.13 | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +0,0 @@ | ||
|
||
|
||
pkg_resources = __import__('pkg_resources') | ||
distribution = pkg_resources.get_distribution('django-typed-models') | ||
|
||
VERSION = __version__ = distribution.version | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
import inspect | ||
from functools import partial | ||
import types | ||
|
||
import django | ||
from django.core.exceptions import FieldDoesNotExist, FieldError | ||
from django.core.serializers.python import Serializer as _PythonSerializer | ||
from django.core.serializers.xml_serializer import Serializer as _XmlSerializer | ||
|
@@ -245,40 +245,79 @@ def _model_has_field(cls, base_class, field_name): | |
def _patch_fields_cache(cls, base_class): | ||
orig_get_fields = cls._meta._get_fields | ||
|
||
def _get_fields( | ||
self, | ||
forward=True, | ||
reverse=True, | ||
include_parents=True, | ||
include_hidden=False, | ||
seen_models=None, | ||
): | ||
cache_key = ( | ||
forward, | ||
reverse, | ||
include_parents, | ||
include_hidden, | ||
seen_models is None, | ||
) | ||
if django.VERSION >= (5, 0): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Change necessary due to the change in signature of |
||
|
||
def _get_fields( | ||
self, | ||
forward=True, | ||
reverse=True, | ||
include_parents=True, | ||
include_hidden=False, | ||
topmost_call=True, | ||
): | ||
cache_key = ( | ||
forward, | ||
reverse, | ||
include_parents, | ||
include_hidden, | ||
topmost_call, | ||
) | ||
|
||
was_cached = cache_key in self._get_fields_cache | ||
fields = orig_get_fields( | ||
forward=forward, | ||
reverse=reverse, | ||
include_parents=include_parents, | ||
include_hidden=include_hidden, | ||
seen_models=seen_models, | ||
) | ||
# If it was cached already, it's because we've already filtered this, skip it | ||
if not was_cached: | ||
fields = [ | ||
f | ||
for f in fields | ||
if TypedModelMetaclass._model_has_field(cls, base_class, f.name) | ||
] | ||
fields = make_immutable_fields_list("get_fields()", fields) | ||
self._get_fields_cache[cache_key] = fields | ||
return fields | ||
was_cached = cache_key in self._get_fields_cache | ||
fields = orig_get_fields( | ||
forward=forward, | ||
reverse=reverse, | ||
include_parents=include_parents, | ||
include_hidden=include_hidden, | ||
topmost_call=topmost_call, | ||
) | ||
# If it was cached already, it's because we've already filtered this, skip it | ||
if not was_cached: | ||
fields = [ | ||
f | ||
for f in fields | ||
if TypedModelMetaclass._model_has_field(cls, base_class, f.name) | ||
] | ||
fields = make_immutable_fields_list("get_fields()", fields) | ||
self._get_fields_cache[cache_key] = fields | ||
return fields | ||
|
||
else: | ||
|
||
def _get_fields( | ||
self, | ||
forward=True, | ||
reverse=True, | ||
include_parents=True, | ||
include_hidden=False, | ||
seen_models=None, | ||
): | ||
cache_key = ( | ||
forward, | ||
reverse, | ||
include_parents, | ||
include_hidden, | ||
seen_models is None, | ||
) | ||
|
||
was_cached = cache_key in self._get_fields_cache | ||
fields = orig_get_fields( | ||
forward=forward, | ||
reverse=reverse, | ||
include_parents=include_parents, | ||
include_hidden=include_hidden, | ||
seen_models=seen_models, | ||
) | ||
# If it was cached already, it's because we've already filtered this, skip it | ||
if not was_cached: | ||
fields = [ | ||
f | ||
for f in fields | ||
if TypedModelMetaclass._model_has_field(cls, base_class, f.name) | ||
] | ||
fields = make_immutable_fields_list("get_fields()", fields) | ||
self._get_fields_cache[cache_key] = fields | ||
return fields | ||
|
||
cls._meta._get_fields = partial(_get_fields, cls._meta) | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I’ve moved the GitHub setup to only use a per-Python matrix, using tox to run the relevant environments per version. This is what I use on my ~30 open source projects and it works really well. It requires less maintenance and is actually faster than splitting per Django version unless you have a long test suite.