django-modeltranslation/modeltranslation/forms.py
wrwrwr 22faea9646 Reworked the solution by replacing clearable widgets with a widget wrapper. This makes it possible to wrap only widgets of nullable fields, and thus not add the clear checkbox when it's not needed.
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.
2013-05-14 22:21:03 +02:00

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)