Skip to main content

Model

Trait Model 

Source
pub trait Model:
    Sized
    + Send
    + Sync
    + 'static {
    type Pk: DbType + Clone + PartialEq + Debug;

    const META: &'static ModelMeta;

    // Required methods
    fn pk(&self) -> Self::Pk;
    fn set_pk(&mut self, value: Value) -> Result<(), QueryError>;
    fn is_unsaved(&self) -> bool;
    fn to_values(&self) -> Vec<(&'static str, Value)>;
    fn from_row(row: &Row, prefix: &str) -> Result<Self, QueryError>;

    // Provided method
    fn objects(db: &Db) -> QuerySet<Self> { ... }
}
Expand description

A database-backed model.

Implemented by #[derive(Model)]. Hand-written implementations must keep to_values and from_row consistent with META.

Required Associated Constants§

Source

const META: &'static ModelMeta

Static metadata.

Required Associated Types§

Source

type Pk: DbType + Clone + PartialEq + Debug

Primary-key type (i64, Uuid, …).

Required Methods§

Source

fn pk(&self) -> Self::Pk

The primary-key value.

Source

fn set_pk(&mut self, value: Value) -> Result<(), QueryError>

Store a primary key generated by the database after an insert.

§Errors

QueryError::Decode if value does not fit the pk type.

Source

fn is_unsaved(&self) -> bool

Whether save must INSERT: true for an auto primary key that still holds its default (e.g. 0). Models with a manual key return false; save then tries UPDATE and falls back to INSERT.

Source

fn to_values(&self) -> Vec<(&'static str, Value)>

Every column in ModelMeta::fields order, as (column, value).

Source

fn from_row(row: &Row, prefix: &str) -> Result<Self, QueryError>

Decode a model from row.

Columns are looked up as prefix + column: prefix is "" for the root model and "author__" for a model loaded via select_related.

§Errors

QueryError::Decode naming the missing or mistyped column.

Provided Methods§

Source

fn objects(db: &Db) -> QuerySet<Self>

Lazy queryset over this model (Django Model.objects).

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§