• <tfoot id='4IWWx'></tfoot>

    1. <small id='4IWWx'></small><noframes id='4IWWx'>

      <legend id='4IWWx'><style id='4IWWx'><dir id='4IWWx'><q id='4IWWx'></q></dir></style></legend>
    2. <i id='4IWWx'><tr id='4IWWx'><dt id='4IWWx'><q id='4IWWx'><span id='4IWWx'><b id='4IWWx'><form id='4IWWx'><ins id='4IWWx'></ins><ul id='4IWWx'></ul><sub id='4IWWx'></sub></form><legend id='4IWWx'></legend><bdo id='4IWWx'><pre id='4IWWx'><center id='4IWWx'></center></pre></bdo></b><th id='4IWWx'></th></span></q></dt></tr></i><div id='4IWWx'><tfoot id='4IWWx'></tfoot><dl id='4IWWx'><fieldset id='4IWWx'></fieldset></dl></div>
        <bdo id='4IWWx'></bdo><ul id='4IWWx'></ul>

        如何限制 Django 模型中数值字段的最大值?

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

              • <bdo id='Ppt4C'></bdo><ul id='Ppt4C'></ul>

                <tfoot id='Ppt4C'></tfoot>

                    <tbody id='Ppt4C'></tbody>
                  <legend id='Ppt4C'><style id='Ppt4C'><dir id='Ppt4C'><q id='Ppt4C'></q></dir></style></legend>

                  <small id='Ppt4C'></small><noframes id='Ppt4C'>

                  <i id='Ppt4C'><tr id='Ppt4C'><dt id='Ppt4C'><q id='Ppt4C'><span id='Ppt4C'><b id='Ppt4C'><form id='Ppt4C'><ins id='Ppt4C'></ins><ul id='Ppt4C'></ul><sub id='Ppt4C'></sub></form><legend id='Ppt4C'></legend><bdo id='Ppt4C'><pre id='Ppt4C'><center id='Ppt4C'></center></pre></bdo></b><th id='Ppt4C'></th></span></q></dt></tr></i><div id='Ppt4C'><tfoot id='Ppt4C'></tfoot><dl id='Ppt4C'><fieldset id='Ppt4C'></fieldset></dl></div>
                • 本文介绍了如何限制 Django 模型中数值字段的最大值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  Django 有各种可用于模型的数字字段,例如DecimalField 和 PositiveIntegerField.虽然前者可以限制为存储的小数位数和存储的字符总数,但有没有办法将其限制为存储一定范围内的 only 数字,例如0.0-5.0 ?

                  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 ?

                  如果做不到这一点,有没有办法限制 PositiveIntegerField 只存储例如最多 50 个数字?

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

                  更新:现在 Bug 6845 已经关闭,这个 StackOverflow 问题可能没有实际意义.- sampablokuper

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

                  推荐答案

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

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

                  在这种情况下,您可以继承"内置 IntegerField 并覆盖其验证逻辑.

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

                  我越想这个,我意识到这对许多 Django 应用程序有多么有用.也许 IntegerRangeField 类型可以作为补丁提交给 Django 开发人员考虑添加到主干.

                  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.

                  这对我有用:

                  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 表示负数和正数的范围(如振荡器范围):

                  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))
                  

                  但是,这将需要更多代码,因为您可以指定一个跳过"参数 - range(1, 50, 2) - 不过这个想法很有趣......

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

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

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  env: python: No such file or directory(env: python: 没有这样的文件或目录)
                  How to evaluate environment variables into a string in Python?(如何在 Python 中将环境变量评估为字符串?)
                  Python - temporarily modify the current process#39;s environment(Python - 临时修改当前进程的环境)
                  Change current process environment#39;s LD_LIBRARY_PATH(更改当前进程环境的 LD_LIBRARY_PATH)
                  Reading and writing environment variables in Python?(在 Python 中读写环境变量?)
                  When to use sys.path.append and when modifying %PYTHONPATH% is enough(何时使用 sys.path.append 以及何时修改 %PYTHONPATH% 就足够了)
                  <i id='cloAB'><tr id='cloAB'><dt id='cloAB'><q id='cloAB'><span id='cloAB'><b id='cloAB'><form id='cloAB'><ins id='cloAB'></ins><ul id='cloAB'></ul><sub id='cloAB'></sub></form><legend id='cloAB'></legend><bdo id='cloAB'><pre id='cloAB'><center id='cloAB'></center></pre></bdo></b><th id='cloAB'></th></span></q></dt></tr></i><div id='cloAB'><tfoot id='cloAB'></tfoot><dl id='cloAB'><fieldset id='cloAB'></fieldset></dl></div>
                  <legend id='cloAB'><style id='cloAB'><dir id='cloAB'><q id='cloAB'></q></dir></style></legend>

                      <tbody id='cloAB'></tbody>

                    • <small id='cloAB'></small><noframes id='cloAB'>

                          <tfoot id='cloAB'></tfoot>
                            <bdo id='cloAB'></bdo><ul id='cloAB'></ul>