File tree Expand file tree Collapse file tree 2 files changed +24
-0
lines changed Expand file tree Collapse file tree 2 files changed +24
-0
lines changed Original file line number Diff line number Diff line change 1+ import json
12import datetime
23import posixpath
34
@@ -149,3 +150,11 @@ def formfield(self, **kwargs):
149150 defaults ['required' ] = False
150151 defaults .update (kwargs )
151152 return super ().formfield (** defaults )
153+
154+ def value_to_string (self , obj ):
155+ value = self .value_from_object (obj )
156+ if isinstance (value , LocalizedFileValue ):
157+ return json .dumps ({k : v .name for k , v
158+ in value .__dict__ .items ()})
159+ else :
160+ return super ().value_to_string (obj )
Original file line number Diff line number Diff line change 22import shutil
33import tempfile as sys_tempfile
44import pickle
5+ import json
56
67from django import forms
78from django .test import TestCase , override_settings
@@ -118,6 +119,20 @@ def test_generate_filename(cls):
118119 filename = field .generate_filename (instance , 'test' , 'en' )
119120 assert filename == 'en_test'
120121
122+ @classmethod
123+ @override_settings (LANGUAGES = (('en' , 'English' ),))
124+ def test_value_to_string (cls ):
125+ """Tests whether the :see:LocalizedFileField
126+ class's :see:value_to_string function works properly."""
127+
128+ temp_file = File (tempfile .NamedTemporaryFile ())
129+ instance = cls .FileFieldModel ()
130+ field = cls .FileFieldModel ._meta .get_field ('file' )
131+ field .upload_to = ''
132+ instance .file .en .save ('testfilename' , temp_file )
133+ expected_value_to_string = json .dumps ({'en' : 'testfilename' })
134+ assert field .value_to_string (instance ) == expected_value_to_string
135+
121136 @staticmethod
122137 def test_get_prep_value ():
123138 """Tests whether the :see:get_prep_value function returns correctly
You can’t perform that action at this time.
0 commit comments