コード例 #1
0
 class Meta:
     database = database
     metadata = metadata
     # define your constraints in Meta class of the model
     # it's a list that can contain multiple constraints
     # hera a combination of name and column will have to be unique in db
     constraints = [ormar.UniqueColumns("name", "completed")]
コード例 #2
0
def update_attrs_from_base_meta(  # noqa: CCR001
    base_class: "Model", attrs: Dict, model_fields: Dict
) -> None:
    """
    Updates Meta parameters in child from parent if needed.

    :param base_class: one of the parent classes
    :type base_class: Model or model parent class
    :param attrs: new namespace for class being constructed
    :type attrs: Dict
    :param model_fields: ormar fields in defined in current class
    :type model_fields: Dict[str, BaseField]
    """

    params_to_update = ["metadata", "database", "constraints"]
    for param in params_to_update:
        current_value = attrs.get("Meta", {}).__dict__.get(param, ormar.Undefined)
        parent_value = (
            base_class.Meta.__dict__.get(param) if hasattr(base_class, "Meta") else None
        )
        if parent_value:
            if param == "constraints":
                verify_constraint_names(
                    base_class=base_class,
                    model_fields=model_fields,
                    parent_value=parent_value,
                )
                parent_value = [
                    ormar.UniqueColumns(*x._pending_colargs) for x in parent_value
                ]
            if isinstance(current_value, list):
                current_value.extend(parent_value)
            else:
                setattr(attrs["Meta"], param, parent_value)
コード例 #3
0
 class Meta:
     abstract = True
     metadata = metadata
     database = db
     constraints = [
         ormar.UniqueColumns("creation_date", "modification_date")
     ]
コード例 #4
0
ファイル: main.py プロジェクト: collerek/sqlalchemy-to-ormar
def _build_model_meta(
    table: Table, metadata: MetaData, database: Database
) -> Type[ormar.ModelMeta]:
    # constraints
    constraints = []
    for const in table.constraints:
        if isinstance(const, sqlalchemy.UniqueConstraint):
            constraints.append(
                ormar.UniqueColumns(*const._pending_colargs)  # type: ignore
            )

    Meta = type(
        "Meta",
        (ormar.ModelMeta,),
        {
            "metadata": metadata,
            "database": database,
            "tablename": table.key,
            "constraints": constraints,
        },
    )
    return cast(Type[ormar.ModelMeta], Meta)
コード例 #5
0
 class Meta(ormar.ModelMeta):
     tablename = "categories"
     constraints = [ormar.UniqueColumns("name", "code")]
コード例 #6
0
 class Meta(BaseMeta):
     tablename = "events"
     constraints = [ormar.UniqueColumns('code')]
コード例 #7
0
ファイル: admin.py プロジェクト: stake-house/poap-reddit-bot
 class Meta(BaseMeta):
     tablename = "admins"
     constraints = [ormar.UniqueColumns('username')]
コード例 #8
0
ファイル: claim.py プロジェクト: stake-house/poap-reddit-bot
 class Meta(BaseMeta):
     tablename = "claims"
     constraints = [ormar.UniqueColumns('attendee', 'event')]
コード例 #9
0
 class Meta:
     tablename = "products"
     metadata = metadata
     database = database
     constraints = [ormar.UniqueColumns("name", "company")]