Django - 在FloatField中禁止NaN和无穷大

问题描述 投票:0回答:2

如何在Django中制作一个FloatField,它不允许保存NaN或Infinity值(同时仍允许空值) - 即某些内容如下:

class MyModel(models.Model):
    rate = models.FloatField(null=True, nans=False) # ???

我使用Postgres作为后端。如果不存在通用解决方案,可能有Postgres特定的解决方案吗?

python django postgresql
2个回答
0
投票

您可以创建自己的自定义字段:

from django.db import models

class HandField(models.FloatField):

    description = "Custom FloatField"

    def check(self, **kwargs):
        errors = super().check(**kwargs)
        errors.extend(self._check_custom_format())
        return errors

def _check_custom_format(self):
    local_errors
    #Here you should check if the format is correct.
    #If something is not right, you should add an error object (from django.core.checks) to local_errors
    return local_errors

错误对象:

checks.Error(
                    'Some error',
                    obj=self,
                    id='Some id',
                )

我没有尝试过编码,基于IntegerField

https://docs.djangoproject.com/en/2.0/howto/custom-model-fields/ https://docs.djangoproject.com/en/2.0/ref/checks/


0
投票

您可以将其添加为约束,也可以创建可以多次使用的类型或域


CREATE DOMAIN plain_float
        AS float /*NOT NULL*/ check( value <> 'nan'::float AND value > '-inf'::float AND value < '+inf'::float )
        ;

CREATE TABLE dummy
        ( id serial PRIMARY KEY
        , val plain_float
        );

\d dummy

INSERT INTO dummy(val) VALUES ( 0.5);   --Ok
INSERT INTO dummy(val) VALUES ('Nan');  --Fails
INSERT INTO dummy(val) VALUES ('-inf');  --Fails
INSERT INTO dummy(val) VALUES ('+inf');  --Fails

SELECT * FROM dummy;

(我不知道如何把它装进你的ORM)


结果:


CREATE DOMAIN
CREATE TABLE
                             Table "tmp.dummy"
 Column |    Type     |                     Modifiers                      
--------+-------------+----------------------------------------------------
 id     | integer     | not null default nextval('dummy_id_seq'::regclass)
 val    | plain_float | 
Indexes:
    "dummy_pkey" PRIMARY KEY, btree (id)

INSERT 0 1
ERROR:  value for domain plain_float violates check constraint "plain_float_check"
ERROR:  value for domain plain_float violates check constraint "plain_float_check"
ERROR:  value for domain plain_float violates check constraint "plain_float_check"
 id | val 
----+-----
  1 | 0.5
(1 row)
© www.soinside.com 2019 - 2024. All rights reserved.