Skip to content

Commit 47de2be

Browse files
authored
Merge pull request #2 from Nabute/feature/dynamic-lisan-model-multilingual-support
Fix: Lisan Model Migration Includes Only lisan_fields
2 parents 3964cf7 + 4109a17 commit 47de2be

File tree

2 files changed

+26
-15
lines changed

2 files changed

+26
-15
lines changed

lisan/metaclasses.py

+25-14
Original file line numberDiff line numberDiff line change
@@ -83,29 +83,40 @@ def __new__(cls, name, bases, attrs):
8383
Returns:
8484
Model: The newly created model class with multilingual support.
8585
"""
86-
# Gather the fields intended for Lisan support
87-
lisan_fields = {
88-
key: value for key, value in attrs.items()
89-
if isinstance(value, models.Field)
90-
}
86+
if 'LisanModelMixin' in [base.__name__ for base in bases]:
87+
lisan_fields = attrs.get('lisan_fields')
88+
89+
# If `lisan_fields` is not defined, raise an exception
90+
if lisan_fields is None:
91+
raise AttributeError(
92+
f"{name} must define 'lisan_fields' when using LisanModelMixin."
93+
)
9194

92-
# Create the new model class
93-
new_class = super().__new__(cls, name, bases, attrs)
95+
# Filter translatable fields by checking if they are defined in lisan_fields
96+
translatable_fields = {
97+
key: value for key, value in attrs.items()
98+
if isinstance(value, models.Field) and key in lisan_fields
99+
}
94100

95-
# If the new class includes LisanModelMixin, generate the Lisan model
96-
if 'LisanModelMixin' in [base.__name__ for base in bases]:
97-
lisan_model = create_lisan_model(new_class, lisan_fields)
101+
# Create the new model class
102+
new_class = super().__new__(cls, name, bases, attrs)
103+
104+
# Generate the Lisan model
105+
lisan_model = create_lisan_model(new_class, translatable_fields)
98106
setattr(new_class, 'Lisan', lisan_model)
99107

100-
# Add a ManyToManyField linking the original model to
101-
# the Lisan model
108+
# Add a ForeignKey linking the original model to the Lisan model
102109
new_class.add_to_class(
103110
'lisans',
104111
models.ManyToManyField(
105112
lisan_model,
106-
related_name="+",
107-
blank=True,
113+
related_name=f"{lisan_model.__name__.lower()}_set",
114+
blank=True
108115
)
109116
)
110117

118+
else:
119+
# Create the new model class if not using the mixin
120+
new_class = super().__new__(cls, name, bases, attrs)
121+
111122
return new_class

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ def read_file(filename):
1111

1212
setup(
1313
name='lisan',
14-
version='0.1.2',
14+
version='0.1.3',
1515
packages=find_packages(),
1616
include_package_data=True,
1717
license='MIT',

0 commit comments

Comments
 (0)