mirror of
https://github.com/Hopiu/django-modeltranslation.git
synced 2026-05-20 18:31:53 +00:00
Added support for inlines.
This commit is contained in:
parent
0cd9a5ddd1
commit
135208d9d1
1 changed files with 60 additions and 4 deletions
|
|
@ -1,3 +1,4 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
from copy import deepcopy
|
||||
|
||||
from django.conf import settings
|
||||
|
|
@ -11,6 +12,7 @@ from django.utils.safestring import mark_safe
|
|||
|
||||
from modeltranslation.translator import translator
|
||||
|
||||
|
||||
class TranslationAdmin(admin.ModelAdmin):
|
||||
|
||||
def patch_translation_field(self, db_field, field, **kwargs):
|
||||
|
|
@ -19,7 +21,6 @@ class TranslationAdmin(admin.ModelAdmin):
|
|||
# Hide the original field by making it non-editable.
|
||||
if db_field.name in trans_opts.fields:
|
||||
db_field.editable = False
|
||||
# field.widget.attrs['readonly'] = "true"
|
||||
|
||||
# For every localized field copy the widget from the original field
|
||||
if db_field.name in trans_opts.localized_fieldnames_rev:
|
||||
|
|
@ -34,14 +35,12 @@ class TranslationAdmin(admin.ModelAdmin):
|
|||
|
||||
field.widget = deepcopy(orig_formfield.widget)
|
||||
|
||||
|
||||
def formfield_for_dbfield(self, db_field, **kwargs):
|
||||
# Call the baseclass function to get the formfield
|
||||
field = super(TranslationAdmin, self).formfield_for_dbfield(db_field, **kwargs)
|
||||
self.patch_translation_field(db_field, field, **kwargs)
|
||||
return field
|
||||
return field
|
||||
|
||||
|
||||
#def save_model(self, request, obj, form, change):
|
||||
#"""
|
||||
#Given a model instance save it to the database.
|
||||
|
|
@ -61,4 +60,61 @@ class TranslationAdmin(admin.ModelAdmin):
|
|||
## Call the baseclass method
|
||||
#super(TranslationAdmin, self).save_model(request, obj, form, change)
|
||||
|
||||
|
||||
class TranslationTabularInline(admin.TabularInline):
|
||||
|
||||
def patch_translation_field(self, db_field, field, **kwargs):
|
||||
trans_opts = translator.get_options_for_model(self.model)
|
||||
|
||||
# Hide the original field by making it non-editable.
|
||||
if db_field.name in trans_opts.fields:
|
||||
db_field.editable = False
|
||||
|
||||
# For every localized field copy the widget from the original field
|
||||
if db_field.name in trans_opts.localized_fieldnames_rev:
|
||||
orig_fieldname = trans_opts.localized_fieldnames_rev[db_field.name]
|
||||
orig_formfield = self.formfield_for_dbfield(self.model._meta.get_field(orig_fieldname), **kwargs)
|
||||
|
||||
# In case the original form field was required, make the default
|
||||
# translation field required instead.
|
||||
if db_field.language == settings.LANGUAGES[0][0] and orig_formfield.required:
|
||||
orig_formfield.required = False
|
||||
field.required = True
|
||||
|
||||
field.widget = deepcopy(orig_formfield.widget)
|
||||
|
||||
def formfield_for_dbfield(self, db_field, **kwargs):
|
||||
# Call the baseclass function to get the formfield
|
||||
field = super(TranslationTabularInline, self).formfield_for_dbfield(db_field, **kwargs)
|
||||
self.patch_translation_field(db_field, field, **kwargs)
|
||||
return field
|
||||
|
||||
|
||||
class TranslationStackedInline(admin.StackedInline):
|
||||
|
||||
def patch_translation_field(self, db_field, field, **kwargs):
|
||||
trans_opts = translator.get_options_for_model(self.model)
|
||||
|
||||
# Hide the original field by making it non-editable.
|
||||
if db_field.name in trans_opts.fields:
|
||||
db_field.editable = False
|
||||
|
||||
# For every localized field copy the widget from the original field
|
||||
if db_field.name in trans_opts.localized_fieldnames_rev:
|
||||
orig_fieldname = trans_opts.localized_fieldnames_rev[db_field.name]
|
||||
orig_formfield = self.formfield_for_dbfield(self.model._meta.get_field(orig_fieldname), **kwargs)
|
||||
|
||||
# In case the original form field was required, make the default
|
||||
# translation field required instead.
|
||||
if db_field.language == settings.LANGUAGES[0][0] and orig_formfield.required:
|
||||
orig_formfield.required = False
|
||||
field.required = True
|
||||
|
||||
field.widget = deepcopy(orig_formfield.widget)
|
||||
|
||||
def formfield_for_dbfield(self, db_field, **kwargs):
|
||||
# Call the baseclass function to get the formfield
|
||||
field = super(TranslationStackedInline, self).formfield_for_dbfield(db_field, **kwargs)
|
||||
self.patch_translation_field(db_field, field, **kwargs)
|
||||
return field
|
||||
|
||||
Loading…
Reference in a new issue