Ejemplo n.º 1
0
    def _construct_model_from_pk(
        cls, value: Any, child: "Model", to_register: bool
    ) -> "Model":
        """
        Takes a pk value, creates a dummy instance and registers it for parent.
        Registration is mutual, so children have also reference to parent.

        Used in normal FK for dictionaries.

        :param value: value of a related pk / fk column
        :type value: Any
        :param child: child/ related Model
        :type child: Model
        :param to_register: flag if the relation should be set in RelationshipManager
        :type to_register: bool
        :return: (if needed) registered Model
        :rtype: Model
        """
        if cls.to.pk_type() == uuid.UUID and isinstance(value, str):
            value = uuid.UUID(value)
        if not isinstance(value, cls.to.pk_type()):
            raise RelationshipInstanceError(
                f"Relationship error - ForeignKey {cls.to.__name__} "
                f"is of type {cls.to.pk_type()} "
                f"while {type(value)} passed as a parameter."
            )
        model = create_dummy_instance(fk=cls.to, pk=value)
        if to_register:
            cls.register_relation(model=model, child=child)
        return model
Ejemplo n.º 2
0
 def _check_if_model_saved(self) -> None:
     """
     Verifies if the parent model of the relation has been already saved.
     Otherwise QuerySetProxy cannot filter by parent primary key.
     """
     pk_value = self._owner.pk
     if not pk_value:
         raise RelationshipInstanceError(
             "You cannot query relationships from unsaved model.")
Ejemplo n.º 3
0
 def resolve_relation_field(
         item: Union["Model", Type["Model"]],
         related: Union["Model", Type["Model"]]) -> Type[BaseField]:
     name = ModelTableProxy.resolve_relation_name(item, related)
     to_field = item.Meta.model_fields.get(name)
     if not to_field:  # pragma no cover
         raise RelationshipInstanceError(
             f"Model {item.__class__} does not have "
             f"reference to model {related.__class__}")
     return to_field
Ejemplo n.º 4
0
 def _construct_model_from_pk(cls, value: Any, child: "Model",
                              to_register: bool) -> "Model":
     if not isinstance(value, cls.to.pk_type()):
         raise RelationshipInstanceError(
             f"Relationship error - ForeignKey {cls.to.__name__} "
             f"is of type {cls.to.pk_type()} "
             f"while {type(value)} passed as a parameter.")
     model = create_dummy_instance(fk=cls.to, pk=value)
     if to_register:
         cls.register_relation(model, child)
     return model
Ejemplo n.º 5
0
 def _set_queryset(self) -> "QuerySet":
     owner_table = self.relation._owner.Meta.tablename
     pkname = self.relation._owner.get_column_alias(self.relation._owner.Meta.pkname)
     pk_value = self.relation._owner.pk
     if not pk_value:
         raise RelationshipInstanceError(
             "You cannot query many to many relationship on unsaved model."
         )
     kwargs = {f"{owner_table}__{pkname}": pk_value}
     queryset = (
         ormar.QuerySet(model_cls=self.relation.to)
         .select_related(owner_table)
         .filter(**kwargs)
     )
     return queryset
Ejemplo n.º 6
0
 def next_alias(self) -> str:
     if not self._next_alias:  # pragma: nocover
         raise RelationshipInstanceError(
             "Alias for given relation not found.")
     return self._next_alias
Ejemplo n.º 7
0
 def next_model(self) -> Type["Model"]:
     if not self._next_model:  # pragma: nocover
         raise RelationshipInstanceError(
             "Cannot link to related table if relation.to model is not set."
         )
     return self._next_model
Ejemplo n.º 8
0
 def through(self) -> Type["Model"]:
     if not self._through:  # pragma: no cover
         raise RelationshipInstanceError(
             "Relation does not have through model!")
     return self._through
Ejemplo n.º 9
0
 def _check_if_model_saved(self) -> None:
     pk_value = self._owner.pk
     if not pk_value:
         raise RelationshipInstanceError(
             "You cannot query relationships from unsaved model."
         )