说我有一个post
具有以下 4 个字段的内容类型:
-
title
(细绳)
-
content
(细绳)
-
slug
(细绳)
-
author
(关系)
如何添加第五个字段,该字段的值取决于上述 4 个字段之一且用户不可编辑?说吧,我想要一个wordCount
字段中的单词数content
字段作为其值。为了合并此功能,我应该考虑探索什么文件?
P.S.:就其价值而言,我正在使用 MongoDB Atlas 来满足我的数据库需求。
你必须创建你的wordCount
内容类型中的属性。
然后在内容管理者左侧菜单中的链接,您将能够自定义编辑/创建页面的视图。在这里你将能够Disable or Remove页面中的字段。
之后您必须进入./api/post/models/Post.js
文件并更新以下功能。
如果您使用NoSQL数据库(Mongo)
beforeSave: async (model) => {
if (model.content) {
model.wordCount = model.content.length;
}
},
beforeUpdate: async (model) => {
if (model.getUpdate().content) {
model.update({
wordCount: model.getUpdate().content.length
});
}
},
如果您使用 SQL(SQLite、Postgres、MySQL)
beforeSave: async (model, attrs, options) => {
if (attrs.content) {
attrs.wordCount = attrs.content.length;
}
},
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)