Adding Models & Views

Adding CRUD Models #

CRUD Models can be added to the AdminInterface which will render a HTML table and the CRUD functions in the View.

Struct Annotations #

The struct for which the view is generated needs to be annotated and at least requires a primary key with the annotation #[actix_admin(primary_key)].

use actix_admin::prelude::*;

pub struct Model {
    #[sea_orm(primary_key)]
    #[serde(skip_deserializing)]
    #[actix_admin(primary_key)]
    pub id: i32,
    pub comment: String
}

// Custom Validation Functions
impl ActixAdminModelValidationTrait<ActiveModel> for Entity {}
// Custom Search Filters
impl ActixAdminModelFilterTrait<Entity> for Entity {}

Derive Implementations #

Actix-Admin relies on two traits to display models which can either be implemented automatically by Macros or manually:

  • ActixAdminViewModelTrait: Handles how the CRUD Tables are displayed
  • ActixAdminModelTrait: Acts as an abstraction between the internal ViewModel and the DB-specific interactions

These can be implemented manually or auto derived by using the following macros:

  • DeriveActixAdmin
  • DeriveActixAdminModel
  • DeriveActixAdminViewModel
use sea_orm::entity::prelude::*;

#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Deserialize, Serialize, 
    DeriveActixAdmin, 
    DeriveActixAdminModel, 
    DeriveActixAdminViewModel
)]
#[sea_orm(table_name = "comment")]
pub struct Model {
    #[sea_orm(primary_key)]
    #[serde(skip_deserializing)]
    #[actix_admin(primary_key)]
    pub id: i32,
    pub comment: String
}

Add the Views to Actix-Admin #

Within the ActixAdminBuilder, the entity can be added as per following code and they will appear in the NavBar in the admin interface.

fn create_actix_admin_builder() -> ActixAdminBuilder {
    let mut admin_builder = ActixAdminBuilder::new(configuration);

    let comment_view_model = ActixAdminViewModel::from(Comment);
    admin_builder.add_entity::<Comment>(&comment_view_model);

    admin_builder
}

Adapt the Views #

let mut post_view_model = ActixAdminViewModel::from(Post);

// clicking on edit will allow editing the row within the table and not redirect to an edit view
post_view_model.inline_edit = true;
// hide the filter list which is open by default
post_view_model.default_show_aside = false;

admin_builder.add_entity::<Post>(&post_view_model);

View Groups #

Views / Models can be grouped in the Navbar by using the following functions instead of admin_builder.add_entity:

admin_builder.add_entity::<Comment>(&comment_view_model);
admin_builder.add_entity_to_category::<Comment>(&comment_view_model, "Group 1");

Additional Model Attributes #

More attributes can be added to the model struct properties:

primary_keyrequireddefines which column is used for the primary key of the model
html_input_type=optionaladd the defined value such as email as input type to the html input field
select_listoptionalA dropdown is rendered for the specific entity, needs to match the name of a struct or an enum
searchableoptionalAdds a search field to the table allowing to search the specific column
textareaoptionalrenders a textarea instead of a text input field
file_uploadoptionalrenders a file upload field, storing the filename in the column, column must be a string
not_emptyoptionaldisallow empty strings such as ""
list_sort_position=optionalorders the columns in the list view by ascending position
list_hide_columnoptionalhides the column in the list view
foreign_key=<entity_name>optionalshows the display of the foreign key entity instead of the id
ceil=optionalceils a float to the given precision
floor=optionalfloor a float to the given precision
dateformat=optionalformats a date or a datetime to the given format, does not work with NaiveDate or NaiveDateTime and requires a timezone
shorten=optionalshortens a string to the given length
use_tom_select_callbackoptionaluses a tom-select.js dropdown instead of a select which allows searching and loads data in the background when searching