Reputation: 58
I have a simple, custom PctField that just multiplies the value by 100 or else sets it to 0.0 if it's null. This works fine in my 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 is 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 my code I was trying to limit the data I returned so I decided to start using .values() on the result and passing in the dynamic list of fields I needed. When I did this, my to_python function in my 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 anyone else seen this before? I'm using django 1.6.8....
Upvotes: 0
Views: 280
Reputation: 57378
It's a known issue, though apparently still under debate after 6 years whether it's a real bug or a documentation bug.
Upvotes: 1