管理中使用 FloatField 而不是 IntegerField
我将数据存储为整数:
class Something(models.Model):
value = models.IntegerField(blank = True)
但我希望允许用户在 django 管理站点 中将其编辑为 FloatField,例如 - 我以美元存储价格作为数字美分,但我希望用户以普通方式编辑价格,然后进行必要的数学运算并将浮点数转换为整数。
这可能吗?如果可以,我该怎么做?
I am storing data as Integers:
class Something(models.Model):
value = models.IntegerField(blank = True)
but I would like to allow user to edit this as FloatField in django admin site, for instance - I am storing price in dollars as the number of cents, but I would like users to edit the price in ordinary way, and then do necessary math and transform floats to integers.
Is this possible, and if so, how would I do that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这都是与形式相关的东西。
阅读小部件和验证。您可以在验证期间替换数据,这就是该方法被称为
clean
的原因。http:// docs.djangoproject.com/en/1.3/topics/forms/modelforms/#overriding-the-default-field-types-or-widgets
http://docs.djangoproject.com/en/1.3/ref/forms/validation/#
This is all form-related stuff.
Read up on widgets and validation. You can replace the data during validation, that's why the method is called
clean
.http://docs.djangoproject.com/en/1.3/topics/forms/modelforms/#overriding-the-default-field-types-or-widgets
http://docs.djangoproject.com/en/1.3/ref/forms/validation/#