Created
November 10, 2010 03:16
-
-
Save valyagolev/670286 to your computer and use it in GitHub Desktop.
Localized Models generator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- 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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
ToDo:
copy.copy
needed?_metaclass_