Repository: deschler/django-modeltranslation

Issue:
TabbedUI not working for ForeignKeys
Hi,

is there a reason, why the tabbed UI does not work for foreign key fields?
![image](https://user-images.githubusercontent.com/18393658/200787514-8e72b99d-d492-400a-9c35-04bf789897cc.png)


If it's a bug and someone hints me in the right direction, I might be able to fix it myself...

Discussion:

Patch:
diff --git a/modeltranslation/admin.py b/modeltranslation/admin.py
index e610216..4430ee6 100644
--- a/modeltranslation/admin.py
+++ b/modeltranslation/admin.py
@@ -94,7 +94,7 @@ class TranslationBaseModelAdmin(BaseModelAdmin):
                 db_field.empty_value == 'both' or orig_field.name in self.both_empty_values_fields
             ) and isinstance(field.widget, (forms.TextInput, forms.Textarea)):
                 field.widget = ClearableWidgetWrapper(field.widget)
-            css_classes = field.widget.attrs.get('class', '').split(' ')
+            css_classes = self._get_widget_from_field(field).attrs.get('class', '').split(' ')
             css_classes.append('mt')
             # Add localized fieldname css class
             css_classes.append(build_css_class(db_field.name, 'mt-field'))
@@ -116,7 +116,14 @@ class TranslationBaseModelAdmin(BaseModelAdmin):
                     # Hide clearable widget for required fields
                     if isinstance(field.widget, ClearableWidgetWrapper):
                         field.widget = field.widget.widget
-            field.widget.attrs['class'] = ' '.join(css_classes)
+            self._get_widget_from_field(field).attrs['class'] = ' '.join(css_classes)
+
+    def _get_widget_from_field(self, field):
+        # retrieve "nested" widget in case of related field
+        if isinstance(field.widget, admin.widgets.RelatedFieldWidgetWrapper):
+            return field.widget.widget
+        else:
+            return field.widget
 
     def _exclude_original_fields(self, exclude=None):
         if exclude is None:


Test patch:
diff --git a/modeltranslation/tests/tests.py b/modeltranslation/tests/tests.py
index 0bbc56c..876bc31 100644
--- a/modeltranslation/tests/tests.py
+++ b/modeltranslation/tests/tests.py
@@ -2172,6 +2172,23 @@ class TranslationAdminTest(ModeltranslationTestBase):
                 in ma.get_form(request, self.test_obj).base_fields.get(field).widget.attrs.values()
             )
 
+    def test_widget_classes_appended_by_formfield_for_dbfield(self):
+        '''
+        regression test for #660 (https://github.com/deschler/django-modeltranslation/issues/660)
+        '''
+        class ForeignKeyModelModelAdmin(admin.TranslationAdmin):
+            fields = ['test']
+
+        class OneToOneFieldModelAdmin(admin.TranslationAdmin):
+            fields = ['test']
+
+        ma = ForeignKeyModelModelAdmin(models.ForeignKeyModel, self.site)
+        fields = ['test_de', 'test_en']
+        for field in fields:
+            assert {} == ma.get_form(request).base_fields.get(field).widget.attrs
+            assert 'class' in ma.get_form(request).base_fields.get(field).widget.widget.attrs.keys()
+            assert 'mt' in ma.get_form(request).base_fields.get(field).widget.widget.attrs['class']
+
     def test_inline_fieldsets(self):
         class DataInline(admin.TranslationStackedInline):
             model = models.DataModel