如何限制Django模型中数字字段的最大值? [英] How to limit the maximum value of a numeric field in a Django model?

查看:361
本文介绍了如何限制Django模型中数字字段的最大值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Django具有可用于模型的各种数字字段,例如。 DecimalField ,=noreferrer>。尽管前者可以被限制为存储的小数位数和存储的总字符数,是否有任何方式来限制它在一定范围内仅存储数字,例如。 0.0-5.0?



没有办法限制一个PositiveIntegerField来存储,例如,最多可以存储50个数字?



更新:现在Bug 6845 已被关闭了,这个StackOverflow问题可能是愚蠢的。 - sampablokuper

解决方案

您还可以创建自定义模型字段类型 - 请参阅 http://docs.djangoproject.com/en/ dev / howto / custom-model-fields /#howto-custom-model-fields



在这种情况下,你可以在IntegerField中,并覆盖其验证逻辑。



我想到的越多,我意识到对于许多Django应用程序来说,这有多有用。也许一个IntegerRangeField类型可以作为Django开发人员考虑添加到trunk的补丁提交。



这对我来说是有用的:

  from django.db import models 

class IntegerRangeField(models.IntegerField):
def __init __(self,verbose_name = None, name = None,min_value = None,max_value = None,** kwargs):
self.min_value,self.max_value = min_value,max_value
models.IntegerField .__ init __(self,verbose_name,name,**
def formfield(self,** kwargs):
defaults = {'min_value':self.min_value,'max_value':self.max_value}
defaults.update(kwargs)
return super(IntegerRangeField,self).formfield(** defaults)

然后在你的模型类,你会像这样使用(字段是放在上面的代码的模块):

  size = fields.IntegerRangeField (min_value = 1,max_value = 50)

或者一个负数和正数范围(如振荡器范围):

  size = fields.IntegerRangeField(min_value = -100,max_value = 100)

真的很酷如果可以使用这样的范围运算符调用它:

  size = fields.IntegerRangeField(range(1,50))

但是,这将需要更多的代码,因为您可以指定一个skip参数范围1,50,2) - 有趣的想法虽然...


Django has various numeric fields available for use in models, e.g. DecimalField and PositiveIntegerField. Although the former can be restricted to the number of decimal places stored and the overall number of characters stored, is there any way to restrict it to storing only numbers within a certain range, e.g. 0.0-5.0 ?

Failing that, is there any way to restrict a PositiveIntegerField to only store, for instance, numbers up to 50?

Update: now that Bug 6845 has been closed, this StackOverflow question may be moot. - sampablokuper

解决方案

You could also create a custom model field type - see http://docs.djangoproject.com/en/dev/howto/custom-model-fields/#howto-custom-model-fields

In this case, you could 'inherit' from the built-in IntegerField and override its validation logic.

The more I think about this, I realize how useful this would be for many Django apps. Perhaps a IntegerRangeField type could be submitted as a patch for the Django devs to consider adding to trunk.

This is working for me:

from django.db import models

class IntegerRangeField(models.IntegerField):
    def __init__(self, verbose_name=None, name=None, min_value=None, max_value=None, **kwargs):
        self.min_value, self.max_value = min_value, max_value
        models.IntegerField.__init__(self, verbose_name, name, **kwargs)
    def formfield(self, **kwargs):
        defaults = {'min_value': self.min_value, 'max_value':self.max_value}
        defaults.update(kwargs)
        return super(IntegerRangeField, self).formfield(**defaults)

Then in your model class, you would use it like this (field being the module where you put the above code):

size = fields.IntegerRangeField(min_value=1, max_value=50)

OR for a range of negative and positive (like an oscillator range):

size = fields.IntegerRangeField(min_value=-100, max_value=100)

What would be really cool is if it could be called with the range operator like this:

size = fields.IntegerRangeField(range(1, 50))

But, that would require a lot more code since since you can specify a 'skip' parameter - range(1, 50, 2) - Interesting idea though...

这篇关于如何限制Django模型中数字字段的最大值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆