python 2.7 - django custom field to_python not called when using .values() -
i have simple, custom pctfield multiplies value 100 or else sets 0.0 if it's null. works fine in code like:
class pctfield(models.floatfield): __metaclass__ = models.subfieldbase def __init__(self, *args, **kwargs): kwargs['blank'] = true kwargs['null'] = true super(pctfield, self).__init__(*args, **kwargs) def to_python(self, value): if value none: return 0. return value * 100 class testmodel(models.model): id = models.integerfield(primary_key=true) non_pct = models.floatfield(blank=true, null=true) pct = pctfield() test = testmodel.objects.get(id=18328) print test.non_pct, test.pct # prints 0.900227, 90.0227 test1 = testmodel.objects.filter(id=18328) print test1[0].non_pct, test1[0].pct # prints 0.900227, 90.0227
later on in code trying limit data returned decided start using .values() on result , passing in dynamic list of fields needed. when did this, to_python function in custom field no longer gets called.
test2 = testmodel.objects.filter(id=18328) print test2.values(*['non_pct', 'pct'])[0] # prints {'non_pct': 0.900227, 'pct': 0.900227}
has else seen before? i'm using django 1.6.8....
it's known issue, though apparently still under debate after 6 years whether it's real bug or documentation bug.
Comments
Post a Comment