Skip to content

Instantly share code, notes, and snippets.

@valyagolev
Created November 10, 2010 03:16
Show Gist options
  • Save valyagolev/670286 to your computer and use it in GitHub Desktop.
Save valyagolev/670286 to your computer and use it in GitHub Desktop.
Localized Models generator
# -*- coding: utf-8 -*-
from django.db import models
from django.contrib import admin
from copy import copy
LANGUAGES = ((2, 'Английский'),)
class LocalizedModel(models.Model):
language = models.IntegerField('язык', choices=LANGUAGES, default=2)
class Meta:
abstract = True
def __unicode__(self):
return '%s (%s)' % (self.object, self.get_language_display())
@classmethod
def get_inline(cls):
class LocalizedModelInline(admin.StackedInline):
max_num = len(LANGUAGES)
model = cls
return LocalizedModelInline
def get_localized_model(model, *fieldnames):
name = model.__name__ + 'Localized'
model_dict = {
'__module__': model.__module__,
'object': models.ForeignKey(model),
}
for field in fieldnames:
model_dict[field] = copy(model._meta.get_field(field))
return type(name, (LocalizedModel,), model_dict)
@valyagolev
Copy link
Author

ToDo:

  • Docs
  • Something with the class name
  • Is copy.copy needed?
  • Try to avoid generating with a model, maybe subclassing with _metaclass_
  • Find a good alternative

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment