2007-10-20 21:01:40 +08:00
|
|
|
import datetime
|
|
|
|
|
2007-09-15 10:37:07 +08:00
|
|
|
from django.db import models
|
|
|
|
|
2007-10-20 21:01:40 +08:00
|
|
|
class BoundaryModel(models.Model):
|
2007-09-15 10:37:07 +08:00
|
|
|
positive_integer = models.PositiveIntegerField(null=True, blank=True)
|
2007-10-20 21:01:40 +08:00
|
|
|
|
|
|
|
class Defaults(models.Model):
|
|
|
|
name = models.CharField(max_length=256, default='class default value')
|
2007-10-23 06:04:00 +08:00
|
|
|
def_date = models.DateField(default = datetime.date(1980, 1, 1))
|
2007-10-20 21:01:40 +08:00
|
|
|
value = models.IntegerField(default=42)
|
|
|
|
|
2007-11-19 04:25:23 +08:00
|
|
|
class ChoiceModel(models.Model):
|
|
|
|
"""For ModelChoiceField and ModelMultipleChoiceField tests."""
|
|
|
|
name = models.CharField(max_length=10)
|
|
|
|
|
2007-09-15 10:37:07 +08:00
|
|
|
__test__ = {'API_TESTS': """
|
2008-07-19 09:22:26 +08:00
|
|
|
>>> from django.forms import form_for_model, form_for_instance
|
2007-09-15 10:37:07 +08:00
|
|
|
|
|
|
|
# Boundary conditions on a PostitiveIntegerField #########################
|
2007-10-20 21:01:40 +08:00
|
|
|
>>> BoundaryForm = form_for_model(BoundaryModel)
|
|
|
|
>>> f = BoundaryForm({'positive_integer':100})
|
|
|
|
>>> f.is_valid()
|
2007-09-15 10:37:07 +08:00
|
|
|
True
|
2007-10-20 21:01:40 +08:00
|
|
|
>>> f = BoundaryForm({'positive_integer':0})
|
|
|
|
>>> f.is_valid()
|
2007-09-15 10:37:07 +08:00
|
|
|
True
|
2007-10-20 21:01:40 +08:00
|
|
|
>>> f = BoundaryForm({'positive_integer':-100})
|
|
|
|
>>> f.is_valid()
|
2007-09-15 10:37:07 +08:00
|
|
|
False
|
|
|
|
|
2007-10-20 21:01:40 +08:00
|
|
|
# Formfield initial values ########
|
|
|
|
If the model has default values for some fields, they are used as the formfield
|
|
|
|
initial values.
|
|
|
|
>>> DefaultsForm = form_for_model(Defaults)
|
|
|
|
>>> DefaultsForm().fields['name'].initial
|
|
|
|
u'class default value'
|
2007-10-23 06:04:00 +08:00
|
|
|
>>> DefaultsForm().fields['def_date'].initial
|
2007-10-20 21:01:40 +08:00
|
|
|
datetime.date(1980, 1, 1)
|
|
|
|
>>> DefaultsForm().fields['value'].initial
|
|
|
|
42
|
|
|
|
|
|
|
|
In form_for_instance(), the initial values come from the instance's values, not
|
|
|
|
the model's defaults.
|
2007-10-23 06:04:00 +08:00
|
|
|
>>> foo_instance = Defaults(name=u'instance value', def_date = datetime.date(1969, 4, 4), value = 12)
|
2007-10-20 21:01:40 +08:00
|
|
|
>>> InstanceForm = form_for_instance(foo_instance)
|
|
|
|
>>> InstanceForm().fields['name'].initial
|
|
|
|
u'instance value'
|
2007-10-23 06:04:00 +08:00
|
|
|
>>> InstanceForm().fields['def_date'].initial
|
2007-10-20 21:01:40 +08:00
|
|
|
datetime.date(1969, 4, 4)
|
|
|
|
>>> InstanceForm().fields['value'].initial
|
|
|
|
12
|
|
|
|
"""}
|