Handling of None / null fields in to_json
It appears that micromodels does not (in my humble opinion) properly handle None / null fields when using to_json.
Modifying micromodels to properly return null when using to_json appears fairly straightforward (and I would happily submit a pull request). Is there some good reason for using the current approach that I'm not aware of? Thanks,
Example Code
import micromodels
class Everything(micromodels.Model):
'''A test of every field type'''
charfield = micromodels.CharField()
integerfield = micromodels.IntegerField()
floatfield = micromodels.FloatField()
booleanfield = micromodels.BooleanField()
datetimefield = micromodels.DateTimeField(format='%m')
datefield = micromodels.DateField(format='%m')
timefield = micromodels.TimeField(format='%m')
empty_data = {
'charfield': None,
'integerfield': None,
'floatfield': None,
'booleanfield': None,
'datetimefield': None,
'datefield': None,
'timefield': None,
}
g = Everything.from_dict(empty_data)
print g.to_json()
Expected Result
{"datefield": null, "integerfield": null, "datetimefield": null, "floatfield": null, "timefield": null, "charfield": null, "booleanfield": null}
Actual Result
charfield returns ""
integerfield returns 0
floatfield returns 0.0
booleanfield returns False
datetimefield returns AttributeError: 'NoneType' object has no attribute 'isoformat'
datefield returns TypeError: NoneType could not be serialized by DateField
timefield returns TypeError: NoneType could not be serialized by TimeField
@j4mie Is this project still active? Just a friendly, sincere question. Thanks very much for your time.
Hi Jeff,
Many thanks for opening the issue. I agree that the case of null data vs missing keys in the data should probably be handled better.
Unfortunately I'm not actively maintaining micromodels at the moment. The project that I was using it for has now ended and so it's difficult for me to find the time to support it. I'd recommend looking at https://github.com/j2labs/dictshield or https://github.com/saymedia/remoteobjects as potential alternatives.
If you'd like to open a pull request I'll take a look at it when I can, but I can't promise when that will be. Alternatively you can fork the project and install your version with pip install git+git://github.com/JeffPaine/micromodels.git.
Cheers
Hello J4mie,
Unfortunately I'm not actively maintaining micromodels at the moment. The project that I was using it for has now ended and so it's difficult for me to find the time to support it.
I totally understand. Thanks very much for filling me in. Surely I would do the same if I was in your position.
Alternatively you can fork the project and install your version with pip install git+git://github.com/JeffPaine/micromodels.git.
Ahh, I did not realize that, nice trick! I'll look into this as an appropriate route forwards, or the other projects you mentioned.
Thanks again for your time,
Hi JeffPaine,
I am sure you have moved on by now but if you still want to work on this, or have solved this problem in a fork. I'd like to include your solution in the next release.
Hello @ericmoritz ,
Thanks much for following up. I ultimately didn't end up attempting to fix this issue. I decided to go with a different solution. I appreciate you checking into it though!