Django 智能小数字段

发布于 2024-12-12 03:23:52 字数 166 浏览 1 评论 0原文

我正在创建一个应用程序,主要存储鞋子尺寸以及其他信息。我一直在使用 Django 小数字段来存储大小,但是小数字段将 10 之类的大小存储为 10.0,所以我的问题是,有没有办法制作或者如果已经存在一个字段,当提供 int ( 10 ) 时它将值存储为 int,并在提供小数时存储 (10.5) 然后将其存储为十进制?

I am creating an app that basically stores shoe sizes, along with other information. I have been using the Django Decimal field to store the sizes, however the decimal field stores sizes like 10 as 10.0, so my question is, is there a way to make or if already exists a field that when provided an int ( 10 ) it stores the value as int and when provided a decimal
( 10.5 ) then stores it as decimal ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

清浅ˋ旧时光 2024-12-19 03:23:53

TBH 对于鞋码字段,我可能会使用带有选择属性的 CharField - 有效鞋码的数量相对较少,这样您就可以按照您想要的方式格式化它们。

如果它是产品模型的一部分,可能包含鞋子以外的东西,则“尺寸”的 CharField 很有用,因为您还可以存储“M”、“L”、“XXL”等值。

您不需要这样做任何有关鞋子尺码的数学计算,因此没有必要使用 DecimalField。

因此,数据库中保存的内容并不重要,重要的是您显示的内容,@j_syk 在评论中提出了一些很好的替代建议。

TBH for a shoe size field I'd probably use a CharField with choices attribute - there are a relatively small number of valid shoe sizes and that way you can format them how you want to.

If it's part of a Product model that may contain things other than shoes, a CharField for 'size' is useful because you can also store values like 'M', 'L', 'XXL' etc.

You won't need to do any math on the shoe sizes so there's no point to using a DecimalField.

For that reason it doesn't really matter what is saved in the db, only what you display and @j_syk had a couple of good alternative suggestions along those lines in the comments.

噩梦成真你也成魔 2024-12-19 03:23:53

简单地将其转换为字符串不适合您吗?您可以保持存储不变,但在呈现数据时根据您的喜好设置数字格式。

>>> str(Decimal(10))
'10'
>>> str(Decimal(10.5))
'10.5'
>>> str(Decimal(10.0))
'10'

Doesn't simply casting it to a string work for you? You can keep your storage as-is, but format the number to your liking when presenting your data.

>>> str(Decimal(10))
'10'
>>> str(Decimal(10.5))
'10.5'
>>> str(Decimal(10.0))
'10'
慈悲佛祖 2024-12-19 03:23:53

我采用了这个解决方案:

  1. 创建一个自定义 Django 模型字段
  2. 其中返回一个自定义的 decimal.Decimal 实例,该实例实现了 __unicode__ 中的技巧,因为这只是一个表示问题

这是代码片段:

class PyPrettyDecimal(decimal.Decimal): 

    def __unicode__(self): 
        # TODO: to improve with decimal properties ?!? 
        mod = self - int(self) 
        if not mod: 
            rv = int(self) 
        else: 
            rv = self.quantize(mod.normalize()) 
        return unicode(rv) 

class PrettyDecimalField(models.DecimalField): 

    __metaclass__ = models.SubfieldBase 

    def to_python(self, value): 
        if value is None: 
            return value 
        try: 
            return PyPrettyDecimal(value) 
        except decimal.InvalidOperation: 
            raise exceptions.ValidationError(self.error_messages['invalid']) 

I adopted this solution:

  1. Create a custom Django model Field
  2. Which return a custom decimal.Decimal instance that implements the trick in __unicode__ because this is just a matter of representation

Here is the snippet:

class PyPrettyDecimal(decimal.Decimal): 

    def __unicode__(self): 
        # TODO: to improve with decimal properties ?!? 
        mod = self - int(self) 
        if not mod: 
            rv = int(self) 
        else: 
            rv = self.quantize(mod.normalize()) 
        return unicode(rv) 

class PrettyDecimalField(models.DecimalField): 

    __metaclass__ = models.SubfieldBase 

    def to_python(self, value): 
        if value is None: 
            return value 
        try: 
            return PyPrettyDecimal(value) 
        except decimal.InvalidOperation: 
            raise exceptions.ValidationError(self.error_messages['invalid']) 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文