method used to construct instances from query results this is where polymorphic deserialization occurs
(cls, values)
| 447 | |
| 448 | @classmethod |
| 449 | def _construct_instance(cls, values): |
| 450 | """ |
| 451 | method used to construct instances from query results |
| 452 | this is where polymorphic deserialization occurs |
| 453 | """ |
| 454 | # we're going to take the values, which is from the DB as a dict |
| 455 | # and translate that into our local fields |
| 456 | # the db_map is a db_field -> model field map |
| 457 | if cls._db_map: |
| 458 | values = dict((cls._db_map.get(k, k), v) for k, v in values.items()) |
| 459 | |
| 460 | if cls._is_polymorphic: |
| 461 | disc_key = values.get(cls._discriminator_column_name) |
| 462 | |
| 463 | if disc_key is None: |
| 464 | raise PolymorphicModelException('discriminator value was not found in values') |
| 465 | |
| 466 | poly_base = cls if cls._is_polymorphic_base else cls._polymorphic_base |
| 467 | |
| 468 | klass = poly_base._get_model_by_discriminator_value(disc_key) |
| 469 | if klass is None: |
| 470 | poly_base._discover_polymorphic_submodels() |
| 471 | klass = poly_base._get_model_by_discriminator_value(disc_key) |
| 472 | if klass is None: |
| 473 | raise PolymorphicModelException( |
| 474 | 'unrecognized discriminator column {0} for class {1}'.format(disc_key, poly_base.__name__) |
| 475 | ) |
| 476 | |
| 477 | if not issubclass(klass, cls): |
| 478 | raise PolymorphicModelException( |
| 479 | '{0} is not a subclass of {1}'.format(klass.__name__, cls.__name__) |
| 480 | ) |
| 481 | |
| 482 | values = dict((k, v) for k, v in values.items() if k in klass._columns.keys()) |
| 483 | |
| 484 | else: |
| 485 | klass = cls |
| 486 | |
| 487 | instance = klass(**values) |
| 488 | instance._set_persisted(force=True) |
| 489 | return instance |
| 490 | |
| 491 | def _set_persisted(self, force=False): |
| 492 | # ensure we don't modify to any values not affected by the last save/update |
nothing calls this directly
no test coverage detected