I'm working on implementation of I18NCharField, so far I come with this:
class I18NCharField(RelatedField):
def __init__(self, *args, **kwargs):
self.from_ = None
self.maxlength = kwargs['maxlength']
super(I18NCharField, self).__init__(*args, **kwargs)
#
def contribute_to_class(self, cls, name):
self.from_ = cls
self.name = name
dispatcher.connect(
self.from_prepared,
signal = signals.class_prepared,
sender = self.from_
)
#
def from_prepared(self):
from django.db import models
from django.utils.translation import gettext_lazy as _
from django.conf.settings import LANGUAGES
id_from = self.from_._meta.db_table
class I18NC(models.Model):
__module__ = self.from_.__module__
language = models.CharField(_('language'), maxlength=3,
choices=LANGUAGES, db_index=True)
text = models.CharField(_('field'),
maxlength=self.maxlength, core=True)
#
I18NC.add_to_class(id_from, models.ForeignKey(self.from_,
verbose_name=self.name, edit_inline=models.TABULAR,
num_in_admin=1, max_num_in_admin=len
(LANGUAGES)))
I18NC._meta.db_table = '%s_i18n_%s' %
(self.from_._meta.db_table, self.name)
I18NC._meta.unique_together = ((id_from,'language'),)
I18NC.__name__ = "I18N_%s_%s" % (id_from, self.name)
I18NC._meta.module_name = ("I18N_%s_%s" % (id_from,
self.name)).lower() + 's'
#
#
Is this is right way to make a new filed?
---
Nebojša Đorđević - nesh
Studio Quattro - Niš - SCG
http://djnesh.blogspot.com/ | http://djnesh-django.blogspot.com/ |
http://djangoutils.python-hosting.com/
Registered Linux User 282159 [http://counter.li.org]