Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You could also create a custom model field type - see <a href="http://docs.djangoproject.com/en/dev/howto/custom-model-fields/#howto-custom-model-fields" rel="noreferrer">http://docs.djangoproject.com/en/dev/howto/custom-model-fields/#howto-custom-model-fields</a></p> <p>In this case, you could 'inherit' from the built-in IntegerField and override its validation logic.</p> <p>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.</p> <p>This is working for me:</p> <pre><code>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) </code></pre> <p>Then in your model class, you would use it like this (field being the module where you put the above code):</p> <pre><code>size = fields.IntegerRangeField(min_value=1, max_value=50) </code></pre> <p>OR for a range of negative and positive (like an oscillator range):</p> <pre><code>size = fields.IntegerRangeField(min_value=-100, max_value=100) </code></pre> <p>What would be really cool is if it could be called with the range operator like this:</p> <pre><code>size = fields.IntegerRangeField(range(1, 50)) </code></pre> <p>But, that would require a lot more code since since you can specify a 'skip' parameter - range(1, 50, 2) - Interesting idea though...</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload