ToUpdateModel

Derive Macro ToUpdateModel 

Source
#[derive(ToUpdateModel)]
{
    // Attributes available to this derive:
    #[crudcrate]
    #[active_model]
}
Expand description

§=================== ToUpdateModel Macro

This macro:

  1. Generates a struct named <OriginalName>Update that includes only the fields where #[crudcrate(update_model = false)] is NOT specified (default = true).
  2. Generates an impl for a method merge_into_activemodel(self, mut model: ActiveModelType) -> ActiveModelType that, for each field:
    • If it’s included in the update struct, and the user provided a value:
      • If the original field type was Option<T>, we match on Option<Option<T>>:
        Some(Some(v)) => ActiveValue::Set(Some(v.into())),
        Some(None)    => ActiveValue::Set(None),     // explicit set to None
        None          => ActiveValue::NotSet,       // no change
      • If the original field type was non‐optional T, we match on Option<T>:
        Some(val) => ActiveValue::Set(val.into()),
        _         => ActiveValue::NotSet,
    • If it’s excluded (update_model = false) but has on_update = expr, we do ActiveValue::Set(expr.into()) (wrapped in Some(...) if the original field was Option<T>).
    • All other fields remain unchanged.