jazzband / jsonmodels

jsonmodels is library to make it easier for you to deal with structures that are converted to, or read from JSON.
http://jsonmodels.readthedocs.org/en/latest/
BSD 3-Clause "New" or "Revised" License
334 stars 51 forks source link

Exception while creating models with ListFields #60

Closed hdantas closed 8 years ago

hdantas commented 8 years ago

I'm unable to instantiate models with ListFields that accept more than one type. For the example below I get the following exception:

jsonmodels.errors.ValidationError: Cannot decide which type to choose from "Cat, Dog".

I'm running the version 2.0.1 of jsonmodels, on Python 2.7.10, OS X 10.11 If I comment line 218 on fields.py everything appears to work correctly, basically removing the call to self._check_items_types_count(). I might be missing something but don't really understand why you're checking if the len of items_types is one.

Here is the complete code to reproduce the issue described:

from jsonmodels import models, fields, errors, validators

class Cat(models.Base):
    name = fields.StringField(required=True)
    breed = fields.StringField()

class Dog(models.Base):
    name = fields.StringField(required=True)
    age = fields.IntField()

class Car(models.Base):
    registration_number = fields.StringField(required=True)
    engine_capacity = fields.FloatField()
    color = fields.StringField()

class Person(models.Base):
    name = fields.StringField(required=True)
    surname = fields.StringField(required=True)
    car = fields.EmbeddedField(Car)
    pets = fields.ListField(items_types=[Cat, Dog])

cat = Cat(name='Garfield')
dog = Dog(name='Dogmeat', age=9)
car = Car(registration_number='ASDF 777', color='red')

person = Person(name='Johny', surname='Bravo', car=car, pets=[cat, dog])
print person.to_struct()
beregond commented 8 years ago

Ok, thanks for feedback - I'll look into this closer.

beregond commented 8 years ago

Fixed in d9f558f

beregond commented 8 years ago

@hdantas so it was fixed - it is already on master branch, if you need it asap. It will be released soon with 2.1, hopefully in few days - I'll inform you in this ticket about it :)

hdantas commented 8 years ago

Great, thanks for your efforts :+1:

beregond commented 8 years ago

@hdantas already available through PyPI as 2.1.