PynamoDB
PynamoDB copied to clipboard
to_json should initialize None when doesn't exist in database
to_json doen't project null fields. In this case, to_json() have important differences with the model.
class Holding(MapAttribute):
id_holding= NumberAttribute()
available = NumberAttribute(null=True)
class HoldingModel(Model):
id_number = NumberAttribute(hash_key=True)
holdings = ListAttribute(of=Holding)
# doesnt work
for object in HoldingModel.query(1):
object.to_json()["holdings"][0]['available']
# work
for object in HoldingModel.query(1):
object..holdings[0].available
As available doesn't exist in dynamodb (because null fields aren't saved), it doesn't exist in my model.
if the available have default=None, in the json the available doesn't exist, if the default is 0, in the json the available exist