mirror of
https://github.com/SectorLabs/django-localized-fields.git
synced 2025-10-29 18:18:57 +03:00
Set None in case the LocalizedIntegerField is null
In case the LocalizedIntegerField is null in the DB then it must explicitly be set to None, otherwise it will yield TypeError: __str__ returned non-string
This commit is contained in:
@@ -6,7 +6,7 @@ from django.conf import settings
|
||||
from django import forms
|
||||
from django.contrib.admin import widgets
|
||||
|
||||
from .value import LocalizedValue
|
||||
from .value import LocalizedValue, LocalizedIntegerValue
|
||||
|
||||
|
||||
class LocalizedFieldWidget(forms.MultiWidget):
|
||||
@@ -52,6 +52,7 @@ class LocalizedFieldWidget(forms.MultiWidget):
|
||||
return result
|
||||
|
||||
def get_context(self, name, value, attrs):
|
||||
value = self.remove_if_needed(value)
|
||||
context = super(forms.MultiWidget, self).get_context(name, value, attrs)
|
||||
if self.is_localized:
|
||||
for widget in self.widgets:
|
||||
@@ -98,6 +99,16 @@ class LocalizedFieldWidget(forms.MultiWidget):
|
||||
|
||||
return attrs
|
||||
|
||||
@staticmethod
|
||||
def remove_if_needed(value):
|
||||
"""If the field LocalizedIntegerField is null in the DB then it must
|
||||
be set to None so it can be represented"""
|
||||
if isinstance(value, LocalizedIntegerValue):
|
||||
not_none_score = list(filter(lambda x: value[x] is not None, [i[0] for i in settings.LANGUAGES]))
|
||||
return value if len(not_none_score) > 0 else None
|
||||
else:
|
||||
return value
|
||||
|
||||
|
||||
class LocalizedCharFieldWidget(LocalizedFieldWidget):
|
||||
"""Widget that has an input box for every language."""
|
||||
|
||||
Reference in New Issue
Block a user