diff --git a/model.go b/model.go index af5b5c4..c1f9965 100644 --- a/model.go +++ b/model.go @@ -1025,7 +1025,11 @@ func (m *Model) Import(w http.ResponseWriter, r *http.Request) { return } //这里用background的context - childCtx := context.WithValue(context.Background(), RuntimeScopeKey, &types.RuntimeScope{ + pCtx := m.db.Statement.Context + if pCtx == nil { + pCtx = context.Background() + } + childCtx := context.WithValue(pCtx, RuntimeScopeKey, &types.RuntimeScope{ Domain: domainName, User: m.valueLookup(types.FieldUser, w, r), ModuleName: m.naming.ModuleName, diff --git a/rest.go b/rest.go index 13924d2..3e2ae61 100644 --- a/rest.go +++ b/rest.go @@ -844,3 +844,13 @@ func OnAfterExport(cb AfterExport) { func OnAfterImport(cb AfterImport) { hookMgr.AfterImport(cb) } + +// RuntimeScopeFromContext 从上下文中获取运行时作用域 +func RuntimeScopeFromContext(ctx context.Context) *types.RuntimeScope { + if v := ctx.Value(RuntimeScopeKey); v != nil { + if scope, ok := v.(*types.RuntimeScope); ok { + return scope + } + } + return nil +}