Revision: 158
Author: eschler
Date: Fri Oct 12 01:27:22 2012
Log: Added support for FileField and ImageField. Resolves issue 30
(thanks to Bruno Tavares and Maxime Haineault).
http://code.google.com/p/django-modeltranslation/source/detail?r=158
Modified:
/trunk/AUTHORS.txt
/trunk/CHANGELOG.txt
/trunk/modeltranslation/fields.py
=======================================
--- /trunk/AUTHORS.txt Thu Oct 11 08:00:54 2012
+++ /trunk/AUTHORS.txt Fri Oct 12 01:27:22 2012
@@ -10,3 +10,4 @@
Bojan Mihelac
Sébastien Fievet
Jacek Tomaszewski
+Bruno Tavares
=======================================
--- /trunk/CHANGELOG.txt Mon Aug 6 04:51:16 2012
+++ /trunk/CHANGELOG.txt Fri Oct 12 01:27:22 2012
@@ -1,6 +1,9 @@
v0.4.0-alpha1
=============
+ ADDED: Support for FileField and ImageField.
+ (thanks to Bruno Tavares,
+ resolves issue 30)
ADDED: New management command sync_database_fields to sync the database
after
a new model has been registered or a new language has been added.
(thanks to Sébastien Fievet and the authors of django-transmeta,
=======================================
--- /trunk/modeltranslation/fields.py Wed Jul 4 03:09:22 2012
+++ /trunk/modeltranslation/fields.py Fri Oct 12 01:27:22 2012
@@ -1,17 +1,17 @@
# -*- coding: utf-8 -*-
-import sys
-from warnings import warn
-
-from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
-from django.db.models.fields import Field, CharField, TextField
+from django.db.models.fields import CharField, TextField
+from django.db.models.fields.files import FileField, ImageField
-from modeltranslation.settings import *
+from modeltranslation.settings import CUSTOM_FIELDS, DEFAULT_LANGUAGE
from modeltranslation.utils import (get_language,
build_localized_fieldname,
build_localized_verbose_name)
+SUPPORTED_FIELDS = (CharField, TextField, FileField, ImageField,)
+
+
def create_translation_field(model, field_name, lang):
"""
Translation field factory. Returns a ``TranslationField`` based on a
@@ -22,20 +22,26 @@
MODELTRANSLATION_CUSTOM_FIELDS = ('MyField', 'MyOtherField',)
- If the class is neither a subclass of CharField or TextField, nor
+ If the class is neither a subclass of fields in ``SUPPORTED_FIELDS``,
nor
in ``CUSTOM_FIELDS`` an ``ImproperlyConfigured`` exception will be
raised.
"""
field = model._meta.get_field(field_name)
cls_name = field.__class__.__name__
- # No subclass required for text-like fields
- if not (isinstance(field, (CharField, TextField)) or
+ if not (isinstance(field, SUPPORTED_FIELDS) or
cls_name in CUSTOM_FIELDS):
raise ImproperlyConfigured('%s is not supported by '
'modeltranslation.' % cls_name)
- return TranslationField(translated_field=field, language=lang)
+ translation_class = field_factory(field.__class__)
+ return translation_class(translated_field=field, language=lang)
+
+
+def field_factory(baseclass):
+ class TranslationFieldSpecific(TranslationField, baseclass):
+ pass
+ return TranslationFieldSpecific
-class TranslationField(Field):
+class TranslationField(object):
"""
The translation field functions as a proxy to the original field which
is
wrapped.
@@ -83,7 +89,8 @@
translated_field.verbose_name, language)
def pre_save(self, model_instance, add):
- val = super(TranslationField, self).pre_save(model_instance, add)
+ val = super(self.translated_field.__class__, self).pre_save(
+ model_instance, add)
if DEFAULT_LANGUAGE == self.language and not add:
# Rule is: 3. Assigning a value to a translation field of the
# default language also updates the original field