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

Issue with foreign keys defined in "app.ModelName" format in abstract base classes #730

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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
5 changes: 3 additions & 2 deletions modeltranslation/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,8 +249,9 @@ def __init__(
self.related_query_name = lambda: loc_related_query_name
self.remote_field.related_name = build_localized_fieldname(current, self.language)
self.remote_field.field = self
if hasattr(self.remote_field.model._meta, "_related_objects_cache"):
del self.remote_field.model._meta._related_objects_cache
if not type(self.remote_field.model) == str:
if hasattr(self.remote_field.model._meta, "_related_objects_cache"):
del self.remote_field.model._meta._related_objects_cache

# Django 1.5 changed definition of __hash__ for fields to be fine with hash requirements.
# It spoiled our machinery, since TranslationField has the same creation_counter as its
Expand Down
16 changes: 14 additions & 2 deletions modeltranslation/tests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,27 @@ class NonTranslated(models.Model):
title = models.CharField(gettext_lazy("title"), max_length=255)


class ForeignKeyModel(models.Model):
class AbstractWithForeignKeyModel(models.Model):
test_in_abstract = models.ForeignKey(
"tests.TestModel",
null=True,
related_name="test_fks",
on_delete=models.CASCADE,
)

class Meta:
abstract = True


class ForeignKeyModel(AbstractWithForeignKeyModel, models.Model):
title = models.CharField(gettext_lazy("title"), max_length=255)
test = models.ForeignKey(
TestModel,
null=True,
related_name="test_fks",
on_delete=models.CASCADE,
)
optional = models.ForeignKey(TestModel, blank=True, null=True, on_delete=models.CASCADE)
optional = models.ForeignKey("tests.TestModel", blank=True, null=True, on_delete=models.CASCADE)
hidden = models.ForeignKey(
TestModel,
blank=True,
Expand Down
12 changes: 11 additions & 1 deletion modeltranslation/tests/translation.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,18 @@ class FileFieldsModelTranslationOptions(TranslationOptions):
# ######### Foreign Key / OneToOneField / ManytoManyField testing


@register(models.AbstractWithForeignKeyModel)
class AbstractWithForeignKeyModelTranslationOptions(TranslationOptions):
fields = (
"test_in_abstract",
)


@register(models.ForeignKeyModel)
class ForeignKeyModelTranslationOptions(TranslationOptions):
class ForeignKeyModelTranslationOptions(
AbstractWithForeignKeyModelTranslationOptions,
TranslationOptions
):
fields = (
"title",
"test",
Expand Down