@@ -83,29 +83,40 @@ def __new__(cls, name, bases, attrs):
83
83
Returns:
84
84
Model: The newly created model class with multilingual support.
85
85
"""
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
+ )
91
94
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
+ }
94
100
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 )
98
106
setattr (new_class , 'Lisan' , lisan_model )
99
107
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
102
109
new_class .add_to_class (
103
110
'lisans' ,
104
111
models .ManyToManyField (
105
112
lisan_model ,
106
- related_name = "+ " ,
107
- blank = True ,
113
+ related_name = f" { lisan_model . __name__ . lower () } _set " ,
114
+ blank = True
108
115
)
109
116
)
110
117
118
+ else :
119
+ # Create the new model class if not using the mixin
120
+ new_class = super ().__new__ (cls , name , bases , attrs )
121
+
111
122
return new_class
0 commit comments