验证至少一个模型字段在 Django admin 中有价值
给定以下模型,我如何要求两个字段中至少一个已被赋予值?
class ZipUpload(models.Model):
zip_file = models.FileField(upload_to="/tmp", blank=True,
help_text='Select a file to upload.')
zip_file_path = models.FilePathField(path="/tmp", blank=True,
help_text="A path to a file on the server)
我正在开发一个只有一小部分用户的小型网站,因此我希望仅使用标准管理网站即可完成这项工作。 我考虑过重写 Model.save() 并在那里添加检查,但我不知道如何以一种好的方式提醒用户错误。
Given the following model, how do I require that atleast one of the two fields has been given a value?
class ZipUpload(models.Model):
zip_file = models.FileField(upload_to="/tmp", blank=True,
help_text='Select a file to upload.')
zip_file_path = models.FilePathField(path="/tmp", blank=True,
help_text="A path to a file on the server)
I'm working on a small site with a small set of users, so I was hoping to make this work with just using the standard admin-site.
I have considered overriding Model.save()
, and adding a check there, but then I don't know how to alert the user of the error in a good way.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这种验证就是定制表单的用途。定义一个Form,在Form中编写验证方法。将表单绑定到模型以创建管理界面。
This kind of validation is what a customized Form is for. Define a Form, write validation methods in the Form. Bind the Form to the Model to create the Admin interface.