Integer Max/Min Values

2,671 views
Skip to first unread message

ajohnsen

unread,
May 16, 2009, 6:32:49 PM5/16/09
to Django users
Hello,

I am trying to assign max_value and min_value to a
PositiveIntegerField in my model but am getting the error:

year_built = models.PositiveIntegerField(min_value=1800,
max_value=2100)
TypeError: __init__() got an unexpected keyword argument 'max_value'

Here is the model:

from django.db import models
from django.forms import ModelForm

floor_plan_choices = (
('A', 'Square'),
('B', 'Rectangular'),
('C', 'Round'),
)

direction_choices = (
('360', '360'),
('270', '270'),
('180', '180'),
('90', '90'),
)

class Customer(models.Model):
date_stamp = models.DateTimeField(auto_now_add=True)
order_number = models.PositiveIntegerField(editable=False)
first_name = models.CharField(max_length=30)
last_name = models.CharField(max_length=40)
email = models.EmailField()
email_conf = models.EmailField(verbose_name='Confirm Email')
year_built = models.PositiveIntegerField(min_value=1800,
max_value=2100)
period = models.PositiveIntegerField(editable=False)
direction = models.DecimalField(max_digits=5, decimal_places=2,
choices=direction_choices)
floor_plan = models.CharField(max_length=2,
choices=floor_plan_choices)

class CustomerForm(ModelForm):

class Meta:
model = Customer

def __unicode__(self):
return u'%s %s' % (self.first_name, self.last_name)

Can anyone explain to me what I am doing wrong and how I should be
assigning max and min values?

Daniel Roseman

unread,
May 16, 2009, 7:07:22 PM5/16/09
to Django users
On May 16, 11:32 pm, ajohnsen <asdjohn...@gmail.com> wrote:
> Hello,
>
> I am trying to assign max_value and min_value to a
> PositiveIntegerField in my model but am getting the error:
>
> year_built = models.PositiveIntegerField(min_value=1800,
> max_value=2100)
> TypeError: __init__() got an unexpected keyword argument 'max_value'
<snip>
> Can anyone explain to me what I am doing wrong and how I should be
> assigning max and min values?

It looks like you are confusing form and model fields. The forms
version of IntegerField takes max_value and min_value parameters.
However, models.IntegerField does not take these, hence your error.

Unfortunately, until model validation is available, there's no way of
enforcing this at the model level. You will need to define a modelform
and override the definitino of year_built on the form.
--
DR.

sampablokuper

unread,
May 16, 2009, 9:53:54 PM5/16/09
to Django users
On May 17, 12:07 am, Daniel Roseman <roseman.dan...@googlemail.com>
wrote:
I've been encountering the same problem. It's especially frustrating
because it seems model validation used to be much easier in Django
(see http://www.cotellese.net/2007/12/11/adding-model-field-validation-to-the-django-admin-page/
).

ajohnsen, you might want to see the Q&A I started over at Stack
Overflow recently:
http://stackoverflow.com/questions/849142/how-to-limit-the-maximum-value-of-a-numeric-field-in-a-django-model

Can anyone explain to me why model validation has been cut down so
arbitrarily? I mean, it's odd that one can specify *some* aspects of
what counts as valid for a particular field of a model (for instance:
how many decimal places a number can have; whether a number can be
negative; the maximum length of a character field; etc) but not others
(e.g. the maximum value of a number field).

Are the Django devs working to re-create model validation in a way
that would bring this missing functionality back to Django, or do they
think that people really ought to be breaking DRY by having to put
this stuff in every form that references a model for which you have
custom (though not uncommon) validation requirements? Or is there some
other way to do this that is better than both the aforementioned
options?

Thanks in advance for your help!

Sam

Sam Kuper

unread,
May 16, 2009, 10:05:15 PM5/16/09
to Django users
2009/5/17 sampablokuper <sampab...@googlemail.com>
I've been encountering the same problem. It's especially frustrating
because it seems model validation used to be much easier in Django
(see http://www.cotellese.net/2007/12/11/adding-model-field-validation-to-the-django-admin-page/
).
[...]


Are the Django devs working to re-create model validation in a way
that would bring this missing functionality back to Django, or do they
think that people really ought to be breaking DRY by having to put
this stuff in every form that references a model for which you have
custom (though not uncommon) validation requirements? Or is there some
other way to do this that is better than both the aforementioned
options?

I've just found some discussion of the issue over here: http://groups.google.com/group/django-users/browse_thread/thread/6b2c1df18b73bf55/3ef0cdc1d3039fbc?lnk=raot

I'm pretty astonished that this feature has been pushed back to v1.1 - to my mind, model validation is an absolutely core ability for a web framework to possess. My faith in Django has just been shaken, *hard*.

:(
Reply all
Reply to author
Forward
0 new messages