Commits

eschler  committed d141627

Fixed broken admin fields and fieldsets. Resolves issue 9 (thanks to simoncelen).

  • Participants
  • Parent commits 364532a

Comments (0)

Files changed (2)

File modeltranslation/admin.py

 from django.utils.safestring import mark_safe
 
 from modeltranslation.translator import translator
-
+from modeltranslation.utils import get_translation_fields
 
 class TranslationAdmin(admin.ModelAdmin):
+    def __init__(self, *args, **kwargs):
+        super(TranslationAdmin, self).__init__(*args, **kwargs)
+      
+        # Replace original field with translation field for each language
+        if self.fields or self.fieldsets:
+            trans_opts = translator.get_options_for_model(self.model)
+            if self.fields:
+                fields_new = list(self.fields)
+                for field in self.fields:
+                    if field in trans_opts.fields:
+                        index = fields_new.index(field)
+                        translation_fields = get_translation_fields(field)
+                        fields_new = self.fields[:index] + translation_fields + self.fields[index+1:]
+                self.fields = fields_new
+            
+            if self.fieldsets:
+                fieldsets_new = list(self.fieldsets)
+                for (name, dct) in self.fieldsets:
+                    if 'fields' in dct:
+                        fields_new = list(dct['fields'])
+                        for field in dct['fields']:
+                            if field in trans_opts.fields:
+                                index = fields_new.index(field)
+                                translation_fields = get_translation_fields(field)
+                                fields_new = fields_new[:index] + translation_fields + fields_new[index+1:]
+                        dct['fields'] = fields_new
+                self.fieldsets = fieldsets_new
                 
     def patch_translation_field(self, db_field, field, **kwargs):
         trans_opts = translator.get_options_for_model(self.model)        

File modeltranslation/utils.py

+# -*- coding: utf-8 -*-
 from django.db import models
 from django.conf import settings
 from django.core.exceptions import ValidationError
     """
     Return an active language code that is guaranteed to be in
     settings.LANGUAGES (Django does not seem to guarantee this for
-    us.)
-    
+    us.)    
     """
     lang = _get_language()
     available_languages = [l[0] for l in settings.LANGUAGES]
         return lang
     return available_languages[0]
 
+
+def get_translation_fields(field):
+    """
+    Returns a list of localized fieldnames for a given field.
+    """
+    return [build_localized_fieldname(field, l[0]) for l in settings.LANGUAGES]
+
+
 class TranslationFieldDescriptor(object):
     """
     A descriptor used for the original translated field.
     
     This taken from http://www.djangosnippets.org/snippets/442/
     """    
-    base_kw = dict([(n, getattr(field,n, '_null')) for n in models.fields.Field.__init__.im_func.func_code.co_varnames])
+    base_kw = dict([(n, getattr(field, n, '_null')) for n in models.fields.Field.__init__.im_func.func_code.co_varnames])
     if isinstance(field, models.fields.related.RelatedField):
         rel = base_kw.get('rel')
-        rel_kw = dict([(n, getattr(rel,n, '_null')) for n in rel.__init__.im_func.func_code.co_varnames])
+        rel_kw = dict([(n, getattr(rel, n, '_null')) for n in rel.__init__.im_func.func_code.co_varnames])
         if isinstance(field, models.fields.related.ForeignKey):
             base_kw['to_field'] = rel_kw.pop('field_name')
         base_kw.update(rel_kw)