mirror of
https://github.com/Hopiu/django-modeltranslation.git
synced 2026-04-16 19:21:05 +00:00
The implementation is loosely based on RelatedFieldWidgetWrapper from contrib.admin, but uses a bit more Python magic; tested only with TextInput and Textarea widgets and their admin counterparts, but with a bit of luck should work with other widgets too.
23 lines
750 B
Python
23 lines
750 B
Python
# -*- coding: utf-8 -*-
|
|
from django import forms
|
|
|
|
from modeltranslation.fields import TranslationField
|
|
|
|
|
|
class TranslationModelForm(forms.ModelForm):
|
|
def __init__(self, *args, **kwargs):
|
|
super(TranslationModelForm, self).__init__(*args, **kwargs)
|
|
for f in self._meta.model._meta.fields:
|
|
if f.name in self.fields and isinstance(f, TranslationField):
|
|
del self.fields[f.name]
|
|
|
|
|
|
class NullableField(object):
|
|
"""
|
|
Form field mixin that ensures that ``None`` is not cast to anything (like
|
|
the empty string with ``CharField`` and its derivatives).
|
|
"""
|
|
def to_python(self, value):
|
|
if value is None:
|
|
return value
|
|
return super(NullableField, self).to_python(value)
|