Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handling of None / null fields in to_json #6

Open
JeffPaine opened this issue May 7, 2012 · 5 comments
Open

Handling of None / null fields in to_json #6

JeffPaine opened this issue May 7, 2012 · 5 comments

Comments

@JeffPaine
Copy link

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

@JeffPaine
Copy link
Author

@j4mie Is this project still active? Just a friendly, sincere question. Thanks very much for your time.

@j4mie
Copy link
Owner

j4mie commented May 10, 2012

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

@JeffPaine
Copy link
Author

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,

@ericmoritz
Copy link
Collaborator

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.

@JeffPaine
Copy link
Author

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants