| 1 |
# -*- coding: utf-8 -*- |
|---|
| 2 |
import datetime |
|---|
| 3 |
|
|---|
| 4 |
from django.db import models |
|---|
| 5 |
# Can't import as "forms" due to implementation details in the test suite (the |
|---|
| 6 |
# current file is called "forms" and is already imported). |
|---|
| 7 |
from django import forms as django_forms |
|---|
| 8 |
|
|---|
| 9 |
class BoundaryModel(models.Model): |
|---|
| 10 |
positive_integer = models.PositiveIntegerField(null=True, blank=True) |
|---|
| 11 |
|
|---|
| 12 |
class Defaults(models.Model): |
|---|
| 13 |
name = models.CharField(max_length=256, default='class default value') |
|---|
| 14 |
def_date = models.DateField(default = datetime.date(1980, 1, 1)) |
|---|
| 15 |
value = models.IntegerField(default=42) |
|---|
| 16 |
|
|---|
| 17 |
class ChoiceModel(models.Model): |
|---|
| 18 |
"""For ModelChoiceField and ModelMultipleChoiceField tests.""" |
|---|
| 19 |
name = models.CharField(max_length=10) |
|---|
| 20 |
|
|---|
| 21 |
class FileModel(models.Model): |
|---|
| 22 |
file = models.FileField(upload_to='/') |
|---|
| 23 |
|
|---|
| 24 |
class FileForm(django_forms.Form): |
|---|
| 25 |
file1 = django_forms.FileField() |
|---|
| 26 |
|
|---|
| 27 |
__test__ = {'API_TESTS': """ |
|---|
| 28 |
>>> from django.forms.models import ModelForm |
|---|
| 29 |
>>> from django.core.files.uploadedfile import SimpleUploadedFile |
|---|
| 30 |
|
|---|
| 31 |
# FileModel with unicode filename and data ######################### |
|---|
| 32 |
>>> f = FileForm(data={}, files={'file1': SimpleUploadedFile('我隻氣墊船裝滿晒鱔.txt', 'मेरी मँडराने वाली नाव सर्पमीनों से भरी ह')}, auto_id=False) |
|---|
| 33 |
>>> f.is_valid() |
|---|
| 34 |
True |
|---|
| 35 |
>>> f.cleaned_data |
|---|
| 36 |
{'file1': <SimpleUploadedFile: 我隻氣墊船裝滿晒鱔.txt (text/plain)>} |
|---|
| 37 |
>>> m = FileModel.objects.create(file=f.cleaned_data['file1']) |
|---|
| 38 |
|
|---|
| 39 |
# Boundary conditions on a PostitiveIntegerField ######################### |
|---|
| 40 |
>>> class BoundaryForm(ModelForm): |
|---|
| 41 |
... class Meta: |
|---|
| 42 |
... model = BoundaryModel |
|---|
| 43 |
>>> f = BoundaryForm({'positive_integer': 100}) |
|---|
| 44 |
>>> f.is_valid() |
|---|
| 45 |
True |
|---|
| 46 |
>>> f = BoundaryForm({'positive_integer': 0}) |
|---|
| 47 |
>>> f.is_valid() |
|---|
| 48 |
True |
|---|
| 49 |
>>> f = BoundaryForm({'positive_integer': -100}) |
|---|
| 50 |
>>> f.is_valid() |
|---|
| 51 |
False |
|---|
| 52 |
|
|---|
| 53 |
# Formfield initial values ######## |
|---|
| 54 |
If the model has default values for some fields, they are used as the formfield |
|---|
| 55 |
initial values. |
|---|
| 56 |
>>> class DefaultsForm(ModelForm): |
|---|
| 57 |
... class Meta: |
|---|
| 58 |
... model = Defaults |
|---|
| 59 |
>>> DefaultsForm().fields['name'].initial |
|---|
| 60 |
u'class default value' |
|---|
| 61 |
>>> DefaultsForm().fields['def_date'].initial |
|---|
| 62 |
datetime.date(1980, 1, 1) |
|---|
| 63 |
>>> DefaultsForm().fields['value'].initial |
|---|
| 64 |
42 |
|---|
| 65 |
|
|---|
| 66 |
In a ModelForm that is passed an instance, the initial values come from the |
|---|
| 67 |
instance's values, not the model's defaults. |
|---|
| 68 |
>>> foo_instance = Defaults(name=u'instance value', def_date=datetime.date(1969, 4, 4), value=12) |
|---|
| 69 |
>>> instance_form = DefaultsForm(instance=foo_instance) |
|---|
| 70 |
>>> instance_form.initial['name'] |
|---|
| 71 |
u'instance value' |
|---|
| 72 |
>>> instance_form.initial['def_date'] |
|---|
| 73 |
datetime.date(1969, 4, 4) |
|---|
| 74 |
>>> instance_form.initial['value'] |
|---|
| 75 |
12 |
|---|
| 76 |
"""} |
|---|