在Strapi中创建一个不可编辑的领域
我有一个销售产品的项目。
产品可以具有价格和可选:new_price。我希望能够按Price/new_price组合对它们进行排序,这意味着我需要在DB中进行额外的条目,例如“ final_price”。我有一个想法是在内容型构建器/schemo.json中创建一个不可编辑的字段“ new_price”,然后在lifecycles.js中以“ beforecreate”上的“ beforecreate”上的最终价格更新该字段。但是,我不确定Strapi允许这样做,因为我还没有找到指向文档中指向的资源。如果有一种骇人听闻的方法,请告知。我也向有关如何执行此业务逻辑的所有其他建议开放。
先感谢您。
I have a project that sells products.
A product can have a price and optional: new_price. I would like to be able to sort them by price/new_price combined, meaning that I would need an additional entry in the DB like "final_price". I got an idea to create a non-editable field "new_price" in Content-Type Builder/schema.json and then update that with the final price for the product on "beforeCreate" in the lifecycles.js. However, I am not sure Strapi allows that, as I haven't been able to find a resource pointing in the documentation that it can. If there is a hacky way to do it, please advise. I am open to all kinds of other suggestions on how to do this Business logic as well.
Thank you in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Strapi有一种方法可以使该字段在UI上不可编辑。您可以转到

content-type-builder->您的组件 - >配置视图
。然后单击文本字段。您可以将可编辑的字段作为错误。There is a way in strapi to make the field non editable on UI. You can go to

Content-type-builder-> Your component-> Configure the view
. And click on the text field. You can just make editable field as false.好的,所以我找到了一种“骇人听闻”的方式来做到这一点。我不喜欢它,因为它不是那么美丽而精致,但它可以完成工作。
在产品架构中,我添加了
,我创建了一个新功能,该功能将计算我的最终价格并将其写入“ final_price”属性:
在管理面板中,在产品中的content-type构建器中,我单击“配置视图”并删除已删除显示字段部分的最终价格字段。这使场隐藏着,大声笑。
现在我正在用排序对所有产品进行排序:['final_price:asc']
就是这样。希望它能帮助任何人!
Ok, so I found a "hacky" way to do this. I don't like it, as it is not that beautiful and sophisticated, but it does the job.
In product schema.json I added a
In lifecycles.js, I created a new function that will calculate my final price and write it in "final_price" attribute:
In the admin panel, in Content-Type Builder in Product, I clicked "Configure the view" and deleted the Final Price field from Displayed Fields section. This made the field hidden, lol.
Now I am sorting all products with sort: ['final_price:asc']
That is it.Hope it helps anyone!
我看到了两个选项:
(a)添加该字段并忘记它是不可编辑的。您可以使用LifeCycle Hooks在收藏中插入条目时计算
final_price
: https://docs.strapi.io/developer-developer-docs/latest/development/backend-custend-custend-customization/models.html-wlifecycle-hooks 。(b)不要添加一个新字段,而要覆盖控制器以返回一个其他字段: https://docs.strapi.io/developer-docs/latest/development/backend-custend-customization/backend-customization/controllers.html#Evtml#Evtml#Evtml#ervending-core-core-conterers 。
I see two options:
(a) Add the field and forget about it being non-editable. You can calculate the
final_price
on insertion of an entry in your collection with Lifecycle hooks: https://docs.strapi.io/developer-docs/latest/development/backend-customization/models.html#lifecycle-hooks.(b) Don't add a new field, but override the controller to return an additional field: https://docs.strapi.io/developer-docs/latest/development/backend-customization/controllers.html#extending-core-controllers.