Skip to content

Commit d44a6b8

Browse files
last-partizanWillem Van Onsem
andauthored
fix: Raise ImproperlyConfigured error when fields provided as a string
Co-authored-by: Willem Van Onsem <[email protected]>
1 parent b05260e commit d44a6b8

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

modeltranslation/tests/tests.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import shutil
66
import sys
77
from decimal import Decimal
8-
8+
from modeltranslation.translator import TranslationOptions
99
import pytest
1010
from django import forms
1111
from django.apps import apps as django_apps
@@ -2345,6 +2345,12 @@ def test_multi_inheritance(self):
23452345
assert "titlee" in clsb.fields
23462346
assert 5 == len(clsb.fields) # there are no repetitions
23472347

2348+
def test_str_instead_of_tuple(self):
2349+
with pytest.raises(ImproperlyConfigured):
2350+
2351+
class ModelOptions(TranslationOptions):
2352+
fields = "titlea"
2353+
23482354

23492355
class UpdateCommandTest(ModeltranslationTestBase):
23502356
def test_update_command(self):

modeltranslation/translator.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import annotations
22

3+
from textwrap import dedent
34
from copy import deepcopy
45
from functools import partial
56
from typing import Any, Callable, ClassVar, cast
@@ -79,7 +80,18 @@ class FieldsAggregationMetaClass(type):
7980
fields: Sequence[str]
8081

8182
def __new__(cls, name: str, bases: tuple[type, ...], attrs: dict[str, Any]) -> type:
82-
attrs["fields"] = set(attrs.get("fields", ()))
83+
fields = attrs.get("fields", ())
84+
if isinstance(fields, str):
85+
raise ImproperlyConfigured(
86+
dedent(f"""`fields` property should be list or tuple, received `str`.
87+
88+
Hint: Replace with:
89+
90+
fields = ("{fields}", )
91+
""")
92+
)
93+
else:
94+
attrs["fields"] = set(fields)
8395
for base in bases:
8496
if isinstance(base, FieldsAggregationMetaClass):
8597
attrs["fields"].update(base.fields)

0 commit comments

Comments
 (0)