django - Custom Number Field is not executing "to_python"? -
i new django , python.
in project using customfield defined me can encrypt data values before storing database , decrypt after retreiving (there need encryption in case).
i have gone through git-repository / stackoverflow / google find answer question, there similar questions on stackoverflow none of them got issue. unable fix 2 days, need now.
i have code defining customfield follows -
def get_multiple_of_8(some_value): some_value = str(some_value) while(len(some_value) % 8 != 0): some_value = '0'+some_value return some_value def is_multiple_of_8(input_str): input_str = str(input_str) if(len(input_str) % 8 == 0): return true return false class customfield(models.field): '''to encrypt values in model''' __metaclass__ = models.subfieldbase def __init__(self, *args, **kwargs): self.key = myproject.settings.secret_key[:8] self.des = des.new(self.key, des.mode_ecb) kwargs['max_length'] = kwargs.get('max_length',128) models.field.__init__(self, *args, **kwargs) def get_internal_type(self): return 'charfield' def get_prep_value(self, value): if value none: return none value = "%.2f" % value value = get_multiple_of_8(value) to_return = self.des.encrypt(value) to_return = to_return.decode('latin-1') # to_return = force_unicode(to_return) return to_return def to_python(self, value): value = value.encode('latin-1') if is_multiple_of_8(value): to_return = self.des.decrypt(value) try: to_return = float(to_return) return to_return except: return 0
encryption works perfectly, can see encrypted field values using sqlite3 command line.
error getting :
exception type: invalidoperation exception value: invalid literal decimal: '\xc3\x85\xc3\x84\xc3\xa13\xc2\xa6\x0f(u'
on debugging found "to_python()" not functioning properly. (code source django-fields git). getting error shows encrypted string on screen instead of actual value upon retrieval through views.py !
i found 1 question on stackoverflow having same problem got fixed because of metaclass syntax in different python version. python version 2.7.5 , django 1.4.5, developing site on windows 7.
please me resolve issue, there way debug these kind of issues ?, suggestion accepted, thank in advance...
finally got answer :)
had understand how sqlite database system works!
the problem here is, when database using, let in case. firstly shall format data stored it. once types assigned database table fields, (i don't know why) still can save data of other format (may bug in sqlite, table had integer field still stored 350 characters long string, command line displayed). problem arises when retrieval done, database sw integer value field gets non-supported values. (hence error).
may question shifted 1 in brackets in fore mentioned paragraph ? true (because proved me) or there other reasons ?
Comments
Post a Comment