如何使 QoR 模型中的字段只读?

huangapple go评论150阅读模式
英文:

How to make fields in QoR model readonly?

问题

我有一个数据模型:

type Epg struct {
    gorm.Model
    Uri     string `gorm:"not null;unique"`
    Prefix  string `gorm:"size:64;not null;default:''"`
    Etag    string
    Updated time.Time
    Status  bool `gorm:"default:true"`
}

我在菜单中发布了这个数据模型:

EpgResource := Admin.AddResource(&models.Epg{}, &admin.Config{Menu: []string{"Content"}})

现在我可以通过QoR Admin面板查看和编辑数据。但是我想将EtagUpdatedStatus的值设置为只读,因为它们是由系统更新的。

如果我尝试根据文档将这些字段设置为只读:

EpgResource.Meta(&admin.Meta{Name: "Etag", Type: "Readonly"})

我得到了一个错误。是否可能使数据模型中的某些字段可见但只读?如何做到这一点?

错误日志跟踪。

2017/04/25 01:16:04 Finish [GET] /admin/epgs Took 19.59ms
/usr/local/go/src/text/template/exec.go:433
/usr/local/go/src/text/template/exec.go:536
/usr/local/go/src/text/template/exec.go:668
/usr/local/go/src/reflect/value.go:302
/usr/local/go/src/reflect/value.go:434
/usr/local/go/src/runtime/asm_amd64.s:515
/home/rns/golang/src/github.com/qor/admin/func_map.go:1051
/home/rns/golang/src/github.com/qor/admin/func_map.go:220
/home/rns/golang/src/github.com/qor/admin/func_map.go:236
/home/rns/golang/src/github.com/qor/admin/func_map.go:393
got error when render form template for Etag(Readonly): haven't found form
template for meta Etag

英文:

I have a data model:

type Epg struct {
    gorm.Model
    Uri     string `gorm:";not null;unique"`
    Prefix  string `gorm:"size:64;not null;default:''"`
    Etag    string
    Updated time.Time
    Status  bool `gorm:"default:true"`
}

I publish this data model in menu:

EpgResource := Admin.AddResource(&models.Epg{}, &admin.Config{Menu: []string{"Content"}})

Now I can view and edit data via QoR Admin panel. But I want to make values Etag, Updated, Status to be readonly because they are updated by the system.

If I try to make this fields readonly according documentation:

EpgResource.Meta(&admin.Meta{Name: "Etag", Type: "Readonly"})

I got an error. Is it possible to make some fields in data model visible but readonly? How to do that?

Error log trace.

> 2017/04/25 01:16:04 Finish [GET] /admin/epgs Took 19.59ms
> /usr/local/go/src/text/template/exec.go:433
> /usr/local/go/src/text/template/exec.go:536
> /usr/local/go/src/text/template/exec.go:668
> /usr/local/go/src/reflect/value.go:302
> /usr/local/go/src/reflect/value.go:434
> /usr/local/go/src/runtime/asm_amd64.s:515
> /home/rns/golang/src/github.com/qor/admin/func_map.go:1051
> /home/rns/golang/src/github.com/qor/admin/func_map.go:220
> /home/rns/golang/src/github.com/qor/admin/func_map.go:236
> /home/rns/golang/src/github.com/qor/admin/func_map.go:393
> got error when render form template for Etag(Readonly): haven't found form
> template for meta Etag

答案1

得分: 1

那个特定的错误是由于qor.../metas/form/Etag.tmpl找不到模板文件引起的,这个模板文件可能是用来将Etag渲染到表单中的。(你可以将模板渲染为只读/静态元素,而不是输入框)

英文:

That particular error is being caused by qor not finding a template file at .../metas/form/Etag.tmpl, which is presumably needed to actually render the Etag to the form. (You can make the template render a readonly/static element rather than an input)

答案2

得分: 0

EpgResource.Meta(&admin.Meta{Name: "Etag", Type: "Readonly"}) 只能在 Darwin(Mac)机器上工作,而不能在 Linux 机器上工作。

请执行以下操作:

EpgResource.Meta(&admin.Meta{Name: "Etag", Type: "readonly"})

希望这能帮到你。

参考链接:https://doc.getqor.com/admin/metas/hidden-readonly.html#readonly

英文:

EpgResource.Meta(&admin.Meta{Name: "Etag", Type: "Readonly"}) would work only on darwin (Mac) machines. But, not on linux machines.

Do the following:

EpgResource.Meta(&admin.Meta{Name: "Etag", Type: "readonly"})

I hope this helps.

Reference: https://doc.getqor.com/admin/metas/hidden-readonly.html#readonly

huangapple
  • 本文由 发表于 2017年4月25日 05:11:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/43597796.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定