如何强制保存“空”/未更改的 django 管理内联?
我的一个管理模型中有一些内联,它们具有默认值,在使用“添加另一个...”添加新实例时可能不需要更改这些默认值。不幸的是,除非某些值发生了变化,否则 django 不会将这些内联对象识别为新对象。这迫使我添加一个内联,更改任意值,保存,更改回该值并再次保存以达到所需的效果。
到目前为止,我想到的唯一解决方案是添加一个隐藏的“已更改”字段,在添加新的内联时将通过 JavaScript 填充该字段。因为这感觉很黑客,我希望有一个更优雅的解决方案。
I have some inlines in one of my admin models which have default values which likely won't need to be changed when adding a new instance with "Add another ...". Unfortunately django won't recognize these inline as new objects unless some value has changed. This forces me to add an inline, change an arbitrary value, save, change the value back and save again to reach the desired effect.
The only solution I've come up with so far is to add a hidden 'changed'-field which would be populated via JavaScript when adding a new inline. As this feels very hackish I hope there is a more elegant solution.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我花了很长时间才弄清楚,但实际上非常简单。
It took me quite some time to figure out but it is actually really simple.
@daniel 答案很好,但是如果没有进行任何更改,它会尝试保存已经创建的实例,这是没有必要的,最好像这样使用它:
@daniel answer is great, however it will try to save the instance that is already created ever if no changes is made, which is not necessary, better to use it like:
@Daniels、@HardQuestions' 和 @jonespm 的综合答案:
Combined @Daniels, @HardQuestions' and @jonespm answers: