impl selectlist for foreign key

This commit is contained in:
manuel 2022-08-20 20:54:39 +02:00
parent f5d0364244
commit b56bdf8984
4 changed files with 51 additions and 7 deletions

View File

@ -1,6 +1,7 @@
use sea_orm::entity::prelude::*;
use serde::{Deserialize, Serialize};
use actix_admin::prelude::*;
use super::Post;
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Deserialize, Serialize, DeriveActixAdmin, DeriveActixAdminModel, DeriveActixAdminViewModel)]
#[sea_orm(table_name = "comment")]
@ -15,10 +16,26 @@ pub struct Model {
pub user: String,
#[sea_orm(column_type = "DateTime")]
pub insert_date: DateTime,
pub is_visible: bool
pub is_visible: bool,
#[actix_admin(select_list="Post")]
pub post_id: Option<i32>,
pub my_decimal: Decimal
}
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {}
pub enum Relation {
#[sea_orm(
belongs_to = "super::post::Entity",
from = "Column::PostId",
to = "super::post::Column::Id"
)]
Post,
}
impl Related<super::post::Entity> for Entity {
fn to() -> RelationDef {
Relation::Post.def()
}
}
impl ActiveModelBehavior for ActiveModel {}

View File

@ -1,5 +1,5 @@
// setup
use sea_orm::sea_query::{ColumnDef, TableCreateStatement};
use sea_orm::sea_query::{ForeignKeyCreateStatement, ColumnDef, TableCreateStatement};
use sea_orm::{error::*, sea_query, ConnectionTrait, DbConn, ExecResult};
pub mod comment;
pub mod post;
@ -46,6 +46,16 @@ pub async fn create_post_table(db: &DbConn) -> Result<ExecResult, DbErr> {
.col(ColumnDef::new(comment::Column::User).string().not_null())
.col(ColumnDef::new(comment::Column::InsertDate).date_time().not_null())
.col(ColumnDef::new(comment::Column::IsVisible).boolean().not_null())
.col(ColumnDef::new(comment::Column::MyDecimal).decimal().not_null())
.col(ColumnDef::new(comment::Column::PostId).integer())
.foreign_key(
ForeignKeyCreateStatement::new()
.name("fk-comment-post")
.from_tbl(Comment)
.from_col(comment::Column::PostId)
.to_tbl(Post)
.to_col(post::Column::Id),
)
.to_owned();
create_table(db, &stmt).await

View File

@ -5,7 +5,7 @@ use std::fmt;
use std::fmt::Display;
use std::str::FromStr;
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Deserialize, Serialize, DeriveActixAdmin, DeriveActixAdminViewModel, DeriveActixAdminModel)]
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Deserialize, Serialize, DeriveActixAdmin, DeriveActixAdminViewModel, DeriveActixAdminModel, DeriveActixAdminModelSelectList)]
#[sea_orm(table_name = "post")]
pub struct Model {
#[sea_orm(primary_key)]
@ -24,12 +24,29 @@ pub struct Model {
pub insert_date: Date,
}
impl Display for Model {
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
match &*self {
_ => write!(formatter, "{} {}", &self.title, &self.insert_date),
}
}
}
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {}
pub enum Relation {
#[sea_orm(has_many = "super::comment::Entity")]
Comment,
}
impl Related<super::comment::Entity> for Entity {
fn to() -> RelationDef {
Relation::Comment.def()
}
}
impl ActiveModelBehavior for ActiveModel {}
#[derive(Debug, Clone, PartialEq, EnumIter, DeriveActiveEnum, Deserialize, Serialize, DeriveActixAdminSelectList)]
#[derive(Debug, Clone, PartialEq, EnumIter, DeriveActiveEnum, Deserialize, Serialize, DeriveActixAdminEnumSelectList)]
#[sea_orm(rs_type = "String", db_type = "Enum", enum_name = "tea")]
pub enum Tea {
#[sea_orm(string_value = "EverydayTea")]

View File

@ -56,7 +56,7 @@ fn create_actix_admin_builder() -> ActixAdminBuilder {
let comment_view_model = ActixAdminViewModel::from(Comment);
let configuration = ActixAdminConfiguration {
enable_auth: true,
enable_auth: false,
user_is_logged_in: Some(|session: &Session| -> bool {
let user_info = session.get::<UserInfo>("user_info").unwrap();
user_info.is_some()