mirror of
https://github.com/SectorLabs/django-localized-fields.git
synced 2025-04-25 11:42:54 +03:00
Merge pull request #2 from SectorLabs/localized_model_inited_with_dicts
Allow localized fields to be inited with dicts
This commit is contained in:
commit
d507cbfe75
@ -23,7 +23,11 @@ class LocalizedModel(models.Model):
|
|||||||
continue
|
continue
|
||||||
|
|
||||||
value = getattr(self, field.name, None)
|
value = getattr(self, field.name, None)
|
||||||
|
|
||||||
if not isinstance(value, LocalizedValue):
|
if not isinstance(value, LocalizedValue):
|
||||||
value = LocalizedValue()
|
if isinstance(value, dict):
|
||||||
|
value = LocalizedValue(value)
|
||||||
|
else:
|
||||||
|
value = LocalizedValue()
|
||||||
|
|
||||||
setattr(self, field.name, value)
|
setattr(self, field.name, value)
|
||||||
|
@ -27,3 +27,23 @@ class LocalizedModelTestCase(TestCase):
|
|||||||
obj = cls.TestModel()
|
obj = cls.TestModel()
|
||||||
|
|
||||||
assert isinstance(obj.title, LocalizedValue)
|
assert isinstance(obj.title, LocalizedValue)
|
||||||
|
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def test_model_init_kwargs(cls):
|
||||||
|
"""Tests whether all :see:LocalizedField
|
||||||
|
fields are assigned an empty :see:LocalizedValue
|
||||||
|
instance when the model is instanitiated."""
|
||||||
|
data = {
|
||||||
|
'title': {
|
||||||
|
'en': 'english_title',
|
||||||
|
'ro': 'romanian_title',
|
||||||
|
'nl': 'dutch_title'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
obj = cls.TestModel(**data)
|
||||||
|
|
||||||
|
assert isinstance(obj.title, LocalizedValue)
|
||||||
|
assert obj.title.en == 'english_title'
|
||||||
|
assert obj.title.ro == 'romanian_title'
|
||||||
|
assert obj.title.nl == 'dutch_title'
|
Loading…
x
Reference in New Issue
Block a user