mirror of
https://github.com/SectorLabs/django-localized-fields.git
synced 2025-04-25 03:32:55 +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:
parent
eb2cb6b244
commit
a0ca977cab
@ -279,7 +279,7 @@ Besides ``LocalizedField``, there's also:
|
|||||||
|
|
||||||
Allows storing integers in multiple languages. This works exactly like ``LocalizedField`` except that
|
Allows storing integers in multiple languages. This works exactly like ``LocalizedField`` except that
|
||||||
all values must be integers. Do note that values are stored as strings in your database because
|
all values must be integers. Do note that values are stored as strings in your database because
|
||||||
the backing field type is ``hstore``, which only allows storing integers. The ``LocalizedIntegerField``
|
the backing field type is ``hstore``, which only allows storing strings. The ``LocalizedIntegerField``
|
||||||
takes care of ensuring that all values are integers and converts the stored strings back to integers
|
takes care of ensuring that all values are integers and converts the stored strings back to integers
|
||||||
when retrieving them from the database. Do not expect to be able to do queries such as:
|
when retrieving them from the database. Do not expect to be able to do queries such as:
|
||||||
|
|
||||||
|
@ -6,7 +6,7 @@ from django.conf import settings
|
|||||||
from django import forms
|
from django import forms
|
||||||
from django.contrib.admin import widgets
|
from django.contrib.admin import widgets
|
||||||
|
|
||||||
from .value import LocalizedValue
|
from .value import LocalizedValue, LocalizedIntegerValue
|
||||||
|
|
||||||
|
|
||||||
class LocalizedFieldWidget(forms.MultiWidget):
|
class LocalizedFieldWidget(forms.MultiWidget):
|
||||||
@ -52,6 +52,7 @@ class LocalizedFieldWidget(forms.MultiWidget):
|
|||||||
return result
|
return result
|
||||||
|
|
||||||
def get_context(self, name, value, attrs):
|
def get_context(self, name, value, attrs):
|
||||||
|
value = self.remove_if_needed(value)
|
||||||
context = super(forms.MultiWidget, self).get_context(name, value, attrs)
|
context = super(forms.MultiWidget, self).get_context(name, value, attrs)
|
||||||
if self.is_localized:
|
if self.is_localized:
|
||||||
for widget in self.widgets:
|
for widget in self.widgets:
|
||||||
@ -98,6 +99,16 @@ class LocalizedFieldWidget(forms.MultiWidget):
|
|||||||
|
|
||||||
return attrs
|
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):
|
class LocalizedCharFieldWidget(LocalizedFieldWidget):
|
||||||
"""Widget that has an input box for every language."""
|
"""Widget that has an input box for every language."""
|
||||||
|
Loading…
x
Reference in New Issue
Block a user