GoAdminGroup / go-admin

A golang framework helps gopher to build a data visualization and admin panel in ten minutes
https://www.go-admin.com
Apache License 2.0
8.21k stars 1.34k forks source link

[Question] How create related entities objts in one form? #619

Open Todvaa opened 6 months ago

Todvaa commented 6 months ago

Description

Greetings! I have two related entities whose attributes I display together. Is there a way to create them together in a form? Or make two forms for creating these entities in a row. Thank You!!!

Example code

package models

import (
    "gorm.io/gorm"
)

type Article struct {
    gorm.Model // adds ID, created_at etc.

    SitePosition int `json:"site_position" gorm:"uniqueIndex:page_site_position_uniq"`

    PageName string `json:"page_id" gorm:"type:varchar(25);not null;uniqueIndex:page_site_position_uniq"`
    Page     Page   `json:"page" gorm:"foreignKey:PageName;references:Name"`
}

type ArticleTranslation struct {
    gorm.Model // adds ID, created_at etc.

    Text string `json:"text" gorm:"type:text"`

    LocalizationName string       `json:"localization_name" gorm:"type:char(2);not null;uniqueIndex:localization_article_uniq" validate:"required"`
    Localization     Localization `json:"-" gorm:"foreignKey:LocalizationName;references:Name"`
    ArticleID        uint         `json:"article_id" gorm:"uniqueIndex:localization_article_uniq"`
    Article          Article      `json:"-" gorm:"constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"`
}
package tables

import "github.com/GoAdminGroup/go-admin/template/types"

const (
    ID             = "Номер"
    TranslationsID = "Номер перевода"
    Text           = "Текст"
    Localization   = "Язык"
    SitePosition   = "Позиция на сайте"
    Page           = "Страница размещения"
)
const (
    Articles     = "Тексты"
    ArticlesDesc = "Отдельные тексты на страницах"
)

var LocalizationOptions = types.FieldOptions{
    {Text: "Русский", Value: "ru"},
    {Text: "English", Value: "en"},
}

var PageOptions = types.FieldOptions{
    {Text: "Главная страница", Value: "main"},
    {Text: "Работа у нас", Value: "jobs"},
}
package tables

import (
    "github.com/GoAdminGroup/go-admin/context"
    "github.com/GoAdminGroup/go-admin/modules/db"
    _ "github.com/GoAdminGroup/go-admin/modules/db"
    "github.com/GoAdminGroup/go-admin/plugins/admin/modules/table"
    "github.com/GoAdminGroup/go-admin/template/types"
    "github.com/GoAdminGroup/go-admin/template/types/form"
    _ "github.com/GoAdminGroup/go-admin/template/types/form"
)

func GetArticlesTable(ctx *context.Context) table.Table {

    articles := table.NewDefaultTable(ctx, table.Config{
        Driver:     db.DriverPostgresql,
        CanAdd:     true,
        Editable:   true,
        Deletable:  true,
        Exportable: true,
        Connection: table.DefaultConnectionName,
        PrimaryKey: table.PrimaryKey{
            Type: db.Int,
            Name: "id",
        },
    })

    info := articles.GetInfo()

    info.SetTable("article_translations").SetTitle(Articles).SetDescription(ArticlesDesc)
    info.AddField(ID, "article_id", db.Int)
    info.AddField(Text, "text", db.Text)
    info.AddField(Localization, "localization_name", db.Text)
    info.AddField(SitePosition, "site_position", db.Int).FieldJoin(types.Join{
        Table:     "articles",
        Field:     "article_id",
        JoinField: "id",
    })
    info.AddField(Page, "page_name", db.Varchar).FieldJoin(types.Join{
        Table:     "articles",
        Field:     "article_id",
        JoinField: "id",
    })
    info.AddField(TranslationsID, "id", db.Int)

    formList := articles.GetForm()
    formList.SetTable("article_translations").SetTitle(Articles).SetDescription(ArticlesDesc)
    formList.AddField(ID, "article_id", db.Int, form.Default).FieldDisplayButCanNotEditWhenUpdate().FieldDisableWhenCreate()
    formList.AddField(Localization, "localization_name", db.Text, form.SelectSingle).
        FieldOptions(LocalizationOptions).FieldDisplayButCanNotEditWhenUpdate()
    formList.AddField(Text, "text", db.Text, form.Text)

    SitePosition ????

    Page ????

    return articles
}