You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将自定义Post模型添加至goAdmin仪表盘?模型未显示求助

自定义Post模型无法显示在GoAdmin导航栏的问题

我尝试将自定义Post模型添加至GoAdmin仪表盘,但该模型并未如预期显示在导航栏中。我已按照官方文档完成配置,并运行了Postgres数据库对应的初始化脚本。

模型定义(post-model.go)

type Post struct {
    gorm.Model
    Title       string
    Description string
}

Admin配置(admin.go)

func ConfigureAdmin(router *gin.Engine) {
    eng := engine.Default()
    adminPlugin := admin.NewAdmin(datamodel.Generators)
    adminPlugin.AddGenerator("Post", tables.GetPostTable)
    eng.AddPlugins(adminPlugin)
    cfg := loadConfig()
    _ = eng.AddConfig(&cfg).Use(router)
}


func loadConfig() config.Config {
    cfg := config.Config{
        Databases: config.DatabaseList{
            "default": {
                Host:         os.Getenv("POSTGRES_HOST"),
                Port:         os.Getenv("POSTGRES_PORT"),
                User:         os.Getenv("POSTGRES_USER"),
                Pwd:          os.Getenv("POSTGRES_PASSWORD"),
                Name:         os.Getenv("POSTGRES_DB"),
                MaxIdleConns: 50,
                MaxOpenConns: 150,
                Driver:       os.Getenv("DB_DRIVER"),
            },
        },
        UrlPrefix: "admin",
        Store: config.Store{
            Path:   "./uploads",
            Prefix: "uploads",
        },
        Language: language.EN,
    }
    return cfg
}

Post表配置(post-table.go)

func GetPostTable(ctx *context.Context) table.Table {
    dsn, err := loadEnv()
    if err != nil {
        return nil
    }
    postTable := table.NewDefaultTable(table.DefaultConfigWithDriverAndConnection("postgresql", dsn))
    formList := postTable.GetForm()
    formList.AddField("ID", "id", db.Integer, form.Default)
    formList.AddField("Title", "title", db.Varchar, form.Text)
    formList.AddField("Description", "description", db.Varchar, form.Text)
    return postTable
}

数据库表结构

gorm_db=# \dt
                 List of relations
 Schema |           Name           | Type  | Owner 
--------+--------------------------+-------+-------
 public | goadmin_menu             | table | gorm
 public | goadmin_operation_log    | table | gorm
 public | goadmin_permissions      | table | gorm
 public | goadmin_role_menu        | table | gorm
 public | goadmin_role_permissions | table | gorm
 public | goadmin_role_users       | table | gorm
 public | goadmin_roles            | table | gorm
 public | goadmin_session          | table | gorm
 public | goadmin_site             | table | gorm
 public | goadmin_user_permissions | table | gorm
 public | goadmin_users            | table | gorm
 public | posts                    | table | gorm

当前导航栏结构

Dashboard
Admin >
        Users
        Roles
        Permission
        Menu
        Operation log

说明:通过初始化脚本创建的操作员和管理员账号可在管理面板正常显示,但自定义的Post模型未出现在导航栏中。

内容的提问来源于stack exchange,提问作者kjtn99

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.25 10:20:37