Skip to content
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

fix(compat): Use ForeignObjectRel.hidden field from django 5.1 #756

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,13 @@ jobs:
strategy:
matrix:
python: ["3.9", "3.10", "3.11", "3.12"]
django: ["4.2", "5.0"]
django: ["4.2", "5.0", "5.1"]
database: ["sqlite", "postgres", "mysql"]
exclude:
- python: 3.9
django: 5.0
- python: 3.9
django: 5.1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
DJANGO: ${{ matrix.django }}
Expand Down
18 changes: 18 additions & 0 deletions modeltranslation/_compat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from __future__ import annotations

from typing import TYPE_CHECKING

import django

if TYPE_CHECKING:
from django.db.models.fields.reverse_related import ForeignObjectRel


def is_hidden(field: ForeignObjectRel) -> bool:
return field.hidden


if django.VERSION <= (5, 1):

def is_hidden(field: ForeignObjectRel) -> bool:
return field.is_hidden()
5 changes: 3 additions & 2 deletions modeltranslation/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from modeltranslation.widgets import ClearableWidgetWrapper

from ._typing import Self
from ._compat import is_hidden

SUPPORTED_FIELDS = (
fields.CharField,
Expand Down Expand Up @@ -187,7 +188,7 @@ def __init__(
or self.remote_field.model == self.model
):
self.remote_field.related_name = "%s_rel_+" % self.name
elif self.remote_field.is_hidden():
elif is_hidden(self.remote_field):
# Even if the backwards relation is disabled, django internally uses it, need to use a language scoped related_name
self.remote_field.related_name = "_%s_%s_+" % (
self.model.__name__.lower(),
Expand Down Expand Up @@ -218,7 +219,7 @@ def __init__(
if hasattr(self.remote_field.model._meta, "_related_objects_cache"):
del self.remote_field.model._meta._related_objects_cache

elif self.remote_field and not self.remote_field.is_hidden():
elif self.remote_field and not is_hidden(self.remote_field):
current = self.remote_field.get_accessor_name()
# Since fields cannot share the same rel object:
self.remote_field = copy.copy(self.remote_field)
Expand Down
3 changes: 2 additions & 1 deletion modeltranslation/translator.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
# Re-export the decorator for convenience
from modeltranslation.decorators import register

from ._compat import is_hidden
from ._typing import _ListOrTuple

__all__ = [
Expand Down Expand Up @@ -599,7 +600,7 @@ def _register_single_model(self, model: type[Model], opts: TranslationOptions) -
setattr(model, field.get_attname(), desc)

# Set related field names on other model
if not field.remote_field.is_hidden():
if not is_hidden(field.remote_field):
other_opts = self._get_options_for_model(field.remote_field.model)
other_opts.related = True
other_opts.related_fields.append(field.related_query_name())
Expand Down
Loading