2008-07-19 18:47:59 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2007-10-20 13:01:40 +00:00
|
|
|
import datetime
|
2009-04-05 20:59:20 +00:00
|
|
|
import shutil
|
2010-10-12 03:33:19 +00:00
|
|
|
import tempfile
|
2007-10-20 13:01:40 +00:00
|
|
|
|
2010-10-12 03:33:19 +00:00
|
|
|
from django.db import models
|
2008-07-19 18:47:59 +00:00
|
|
|
# Can't import as "forms" due to implementation details in the test suite (the
|
2008-07-21 03:50:40 +00:00
|
|
|
# current file is called "forms" and is already imported).
|
2008-07-19 18:47:59 +00:00
|
|
|
from django import forms as django_forms
|
2009-01-16 21:31:58 +00:00
|
|
|
from django.core.files.storage import FileSystemStorage
|
2010-01-12 14:58:24 +00:00
|
|
|
from django.test import TestCase
|
2009-01-16 21:31:58 +00:00
|
|
|
|
|
|
|
temp_storage_location = tempfile.mkdtemp()
|
|
|
|
temp_storage = FileSystemStorage(location=temp_storage_location)
|
2007-09-15 02:37:07 +00:00
|
|
|
|
2007-10-20 13:01:40 +00:00
|
|
|
class BoundaryModel(models.Model):
|
2007-09-15 02:37:07 +00:00
|
|
|
positive_integer = models.PositiveIntegerField(null=True, blank=True)
|
2007-10-20 13:01:40 +00:00
|
|
|
|
2010-03-08 15:03:30 +00:00
|
|
|
callable_default_value = 0
|
|
|
|
def callable_default():
|
|
|
|
global callable_default_value
|
|
|
|
callable_default_value = callable_default_value + 1
|
|
|
|
return callable_default_value
|
|
|
|
|
2007-10-20 13:01:40 +00:00
|
|
|
class Defaults(models.Model):
|
2009-05-07 18:06:22 +00:00
|
|
|
name = models.CharField(max_length=255, default='class default value')
|
2007-10-22 22:04:00 +00:00
|
|
|
def_date = models.DateField(default = datetime.date(1980, 1, 1))
|
2007-10-20 13:01:40 +00:00
|
|
|
value = models.IntegerField(default=42)
|
2010-03-08 15:03:30 +00:00
|
|
|
callable_default = models.IntegerField(default=callable_default)
|
2007-10-20 13:01:40 +00:00
|
|
|
|
2007-11-18 20:25:23 +00:00
|
|
|
class ChoiceModel(models.Model):
|
|
|
|
"""For ModelChoiceField and ModelMultipleChoiceField tests."""
|
|
|
|
name = models.CharField(max_length=10)
|
|
|
|
|
2009-05-10 07:44:27 +00:00
|
|
|
class ChoiceOptionModel(models.Model):
|
|
|
|
"""Destination for ChoiceFieldModel's ForeignKey.
|
|
|
|
Can't reuse ChoiceModel because error_message tests require that it have no instances."""
|
|
|
|
name = models.CharField(max_length=10)
|
|
|
|
|
2010-08-14 12:05:41 +00:00
|
|
|
class Meta:
|
|
|
|
ordering = ('name',)
|
|
|
|
|
|
|
|
def __unicode__(self):
|
|
|
|
return u'ChoiceOption %d' % self.pk
|
|
|
|
|
2009-05-10 07:44:27 +00:00
|
|
|
class ChoiceFieldModel(models.Model):
|
|
|
|
"""Model with ForeignKey to another model, for testing ModelForm
|
|
|
|
generation with ModelChoiceField."""
|
|
|
|
choice = models.ForeignKey(ChoiceOptionModel, blank=False,
|
2010-08-14 12:05:41 +00:00
|
|
|
default=lambda: ChoiceOptionModel.objects.get(name='default'))
|
|
|
|
choice_int = models.ForeignKey(ChoiceOptionModel, blank=False, related_name='choice_int',
|
|
|
|
default=lambda: 1)
|
|
|
|
|
|
|
|
multi_choice = models.ManyToManyField(ChoiceOptionModel, blank=False, related_name='multi_choice',
|
|
|
|
default=lambda: ChoiceOptionModel.objects.filter(name='default'))
|
|
|
|
multi_choice_int = models.ManyToManyField(ChoiceOptionModel, blank=False, related_name='multi_choice_int',
|
|
|
|
default=lambda: [1])
|
|
|
|
|
|
|
|
class ChoiceFieldForm(django_forms.ModelForm):
|
|
|
|
class Meta:
|
|
|
|
model = ChoiceFieldModel
|
2009-05-10 07:44:27 +00:00
|
|
|
|
2008-07-19 18:47:59 +00:00
|
|
|
class FileModel(models.Model):
|
2009-01-16 21:31:58 +00:00
|
|
|
file = models.FileField(storage=temp_storage, upload_to='tests')
|
2008-07-19 18:47:59 +00:00
|
|
|
|
|
|
|
class FileForm(django_forms.Form):
|
|
|
|
file1 = django_forms.FileField()
|
|
|
|
|
2010-01-12 14:58:24 +00:00
|
|
|
class Group(models.Model):
|
|
|
|
name = models.CharField(max_length=10)
|
|
|
|
|
|
|
|
def __unicode__(self):
|
|
|
|
return u'%s' % self.name
|
|
|
|
|
|
|
|
class TestTicket12510(TestCase):
|
|
|
|
''' It is not necessary to generate choices for ModelChoiceField (regression test for #12510). '''
|
|
|
|
def setUp(self):
|
|
|
|
self.groups = [Group.objects.create(name=name) for name in 'abc']
|
|
|
|
|
|
|
|
def test_choices_not_fetched_when_not_rendering(self):
|
2010-10-12 03:33:19 +00:00
|
|
|
def test():
|
|
|
|
field = django_forms.ModelChoiceField(Group.objects.order_by('-name'))
|
|
|
|
self.assertEqual('a', field.clean(self.groups[0].pk).name)
|
2010-01-12 14:58:24 +00:00
|
|
|
# only one query is required to pull the model from DB
|
2010-10-12 03:33:19 +00:00
|
|
|
self.assertNumQueries(1, test)
|
2010-01-12 14:58:24 +00:00
|
|
|
|
2010-08-14 12:05:41 +00:00
|
|
|
class ModelFormCallableModelDefault(TestCase):
|
|
|
|
def test_no_empty_option(self):
|
|
|
|
"If a model's ForeignKey has blank=False and a default, no empty option is created (Refs #10792)."
|
|
|
|
option = ChoiceOptionModel.objects.create(name='default')
|
|
|
|
|
|
|
|
choices = list(ChoiceFieldForm().fields['choice'].choices)
|
|
|
|
self.assertEquals(len(choices), 1)
|
|
|
|
self.assertEquals(choices[0], (option.pk, unicode(option)))
|
|
|
|
|
|
|
|
def test_callable_initial_value(self):
|
|
|
|
"The initial value for a callable default returning a queryset is the pk (refs #13769)"
|
|
|
|
obj1 = ChoiceOptionModel.objects.create(id=1, name='default')
|
|
|
|
obj2 = ChoiceOptionModel.objects.create(id=2, name='option 2')
|
|
|
|
obj3 = ChoiceOptionModel.objects.create(id=3, name='option 3')
|
|
|
|
self.assertEquals(ChoiceFieldForm().as_p(), """<p><label for="id_choice">Choice:</label> <select name="choice" id="id_choice">
|
|
|
|
<option value="1" selected="selected">ChoiceOption 1</option>
|
|
|
|
<option value="2">ChoiceOption 2</option>
|
|
|
|
<option value="3">ChoiceOption 3</option>
|
|
|
|
</select><input type="hidden" name="initial-choice" value="1" id="initial-id_choice" /></p>
|
|
|
|
<p><label for="id_choice_int">Choice int:</label> <select name="choice_int" id="id_choice_int">
|
|
|
|
<option value="1" selected="selected">ChoiceOption 1</option>
|
|
|
|
<option value="2">ChoiceOption 2</option>
|
|
|
|
<option value="3">ChoiceOption 3</option>
|
|
|
|
</select><input type="hidden" name="initial-choice_int" value="1" id="initial-id_choice_int" /></p>
|
|
|
|
<p><label for="id_multi_choice">Multi choice:</label> <select multiple="multiple" name="multi_choice" id="id_multi_choice">
|
|
|
|
<option value="1" selected="selected">ChoiceOption 1</option>
|
|
|
|
<option value="2">ChoiceOption 2</option>
|
|
|
|
<option value="3">ChoiceOption 3</option>
|
|
|
|
</select><input type="hidden" name="initial-multi_choice" value="1" id="initial-id_multi_choice_0" /> <span class="helptext"> Hold down "Control", or "Command" on a Mac, to select more than one.</span></p>
|
|
|
|
<p><label for="id_multi_choice_int">Multi choice int:</label> <select multiple="multiple" name="multi_choice_int" id="id_multi_choice_int">
|
|
|
|
<option value="1" selected="selected">ChoiceOption 1</option>
|
|
|
|
<option value="2">ChoiceOption 2</option>
|
|
|
|
<option value="3">ChoiceOption 3</option>
|
|
|
|
</select><input type="hidden" name="initial-multi_choice_int" value="1" id="initial-id_multi_choice_int_0" /> <span class="helptext"> Hold down "Control", or "Command" on a Mac, to select more than one.</span></p>""")
|
|
|
|
|
|
|
|
def test_initial_instance_value(self):
|
|
|
|
"Initial instances for model fields may also be instances (refs #7287)"
|
|
|
|
obj1 = ChoiceOptionModel.objects.create(id=1, name='default')
|
|
|
|
obj2 = ChoiceOptionModel.objects.create(id=2, name='option 2')
|
|
|
|
obj3 = ChoiceOptionModel.objects.create(id=3, name='option 3')
|
|
|
|
self.assertEquals(ChoiceFieldForm(initial={
|
|
|
|
'choice': obj2,
|
|
|
|
'choice_int': obj2,
|
|
|
|
'multi_choice': [obj2,obj3],
|
|
|
|
'multi_choice_int': ChoiceOptionModel.objects.exclude(name="default"),
|
|
|
|
}).as_p(), """<p><label for="id_choice">Choice:</label> <select name="choice" id="id_choice">
|
|
|
|
<option value="1">ChoiceOption 1</option>
|
|
|
|
<option value="2" selected="selected">ChoiceOption 2</option>
|
|
|
|
<option value="3">ChoiceOption 3</option>
|
|
|
|
</select><input type="hidden" name="initial-choice" value="2" id="initial-id_choice" /></p>
|
|
|
|
<p><label for="id_choice_int">Choice int:</label> <select name="choice_int" id="id_choice_int">
|
|
|
|
<option value="1">ChoiceOption 1</option>
|
|
|
|
<option value="2" selected="selected">ChoiceOption 2</option>
|
|
|
|
<option value="3">ChoiceOption 3</option>
|
|
|
|
</select><input type="hidden" name="initial-choice_int" value="2" id="initial-id_choice_int" /></p>
|
|
|
|
<p><label for="id_multi_choice">Multi choice:</label> <select multiple="multiple" name="multi_choice" id="id_multi_choice">
|
|
|
|
<option value="1">ChoiceOption 1</option>
|
|
|
|
<option value="2" selected="selected">ChoiceOption 2</option>
|
|
|
|
<option value="3" selected="selected">ChoiceOption 3</option>
|
|
|
|
</select><input type="hidden" name="initial-multi_choice" value="2" id="initial-id_multi_choice_0" />
|
|
|
|
<input type="hidden" name="initial-multi_choice" value="3" id="initial-id_multi_choice_1" /> <span class="helptext"> Hold down "Control", or "Command" on a Mac, to select more than one.</span></p>
|
|
|
|
<p><label for="id_multi_choice_int">Multi choice int:</label> <select multiple="multiple" name="multi_choice_int" id="id_multi_choice_int">
|
|
|
|
<option value="1">ChoiceOption 1</option>
|
|
|
|
<option value="2" selected="selected">ChoiceOption 2</option>
|
|
|
|
<option value="3" selected="selected">ChoiceOption 3</option>
|
|
|
|
</select><input type="hidden" name="initial-multi_choice_int" value="2" id="initial-id_multi_choice_int_0" />
|
|
|
|
<input type="hidden" name="initial-multi_choice_int" value="3" id="initial-id_multi_choice_int_1" /> <span class="helptext"> Hold down "Control", or "Command" on a Mac, to select more than one.</span></p>""")
|
|
|
|
|
2010-01-12 14:58:24 +00:00
|
|
|
|
2007-09-15 02:37:07 +00:00
|
|
|
__test__ = {'API_TESTS': """
|
Fixed #7830 -- Removed all of the remaining, deprecated, non-oldforms features:
* Support for representing files as strings was removed. Use `django.core.files.base.ContentFile` instead.
* Support for representing uploaded files as dictionaries was removed. Use `django.core.files.uploadedfile.SimpleUploadedFile` instead.
* The `filename`, `file_name`, `file_size`, and `chuck` properties of `UploadedFile` were removed. Use the `name`, `name`, `size`, and `chunks` properties instead, respectively.
* The `get_FIELD_filename`, `get_FIELD_url`, `get_FIELD_size`, and `save_FIELD_file` methods for Models with `FileField` fields were removed. Instead, use the `path`, `url`, and `size` attributes and `save` method on the field itself, respectively.
* The `get_FIELD_width` and `get_FIELD_height` methods for Models with `ImageField` fields were removed. Use the `width` and `height` attributes on the field itself instead.
* The dispatcher `connect`, `disconnect`, `send`, and `sendExact` functions were removed. Use the signal object's own `connect`, `disconnect`, `send`, and `send` methods instead, respectively.
* The `form_for_model` and `form_for_instance` functions were removed. Use a `ModelForm` subclass instead.
* Support for importing `django.newforms` was removed. Use `django.forms` instead.
* Support for importing `django.utils.images` was removed. Use `django.core.files.images` instead.
* Support for the `follow` argument in the `create_object` and `update_object` generic views was removed. Use the `django.forms` package and the new `form_class` argument instead.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@8291 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-08-10 21:10:47 +00:00
|
|
|
>>> from django.forms.models import ModelForm
|
2008-07-19 18:47:59 +00:00
|
|
|
>>> from django.core.files.uploadedfile import SimpleUploadedFile
|
|
|
|
|
|
|
|
# FileModel with unicode filename and data #########################
|
|
|
|
>>> f = FileForm(data={}, files={'file1': SimpleUploadedFile('我隻氣墊船裝滿晒鱔.txt', 'मेरी मँडराने वाली नाव सर्पमीनों से भरी ह')}, auto_id=False)
|
|
|
|
>>> f.is_valid()
|
|
|
|
True
|
|
|
|
>>> f.cleaned_data
|
|
|
|
{'file1': <SimpleUploadedFile: 我隻氣墊船裝滿晒鱔.txt (text/plain)>}
|
|
|
|
>>> m = FileModel.objects.create(file=f.cleaned_data['file1'])
|
2007-09-15 02:37:07 +00:00
|
|
|
|
2009-04-16 14:26:08 +00:00
|
|
|
# It's enough that m gets created without error. Preservation of the exotic name is checked
|
2009-04-05 20:59:20 +00:00
|
|
|
# in a file_uploads test; it's hard to do that correctly with doctest's unicode issues. So
|
|
|
|
# we create and then immediately delete m so as to not leave the exotically named file around
|
|
|
|
# for shutil.rmtree (on Windows) to have trouble with later.
|
|
|
|
>>> m.delete()
|
|
|
|
|
2007-09-15 02:37:07 +00:00
|
|
|
# Boundary conditions on a PostitiveIntegerField #########################
|
Fixed #7830 -- Removed all of the remaining, deprecated, non-oldforms features:
* Support for representing files as strings was removed. Use `django.core.files.base.ContentFile` instead.
* Support for representing uploaded files as dictionaries was removed. Use `django.core.files.uploadedfile.SimpleUploadedFile` instead.
* The `filename`, `file_name`, `file_size`, and `chuck` properties of `UploadedFile` were removed. Use the `name`, `name`, `size`, and `chunks` properties instead, respectively.
* The `get_FIELD_filename`, `get_FIELD_url`, `get_FIELD_size`, and `save_FIELD_file` methods for Models with `FileField` fields were removed. Instead, use the `path`, `url`, and `size` attributes and `save` method on the field itself, respectively.
* The `get_FIELD_width` and `get_FIELD_height` methods for Models with `ImageField` fields were removed. Use the `width` and `height` attributes on the field itself instead.
* The dispatcher `connect`, `disconnect`, `send`, and `sendExact` functions were removed. Use the signal object's own `connect`, `disconnect`, `send`, and `send` methods instead, respectively.
* The `form_for_model` and `form_for_instance` functions were removed. Use a `ModelForm` subclass instead.
* Support for importing `django.newforms` was removed. Use `django.forms` instead.
* Support for importing `django.utils.images` was removed. Use `django.core.files.images` instead.
* Support for the `follow` argument in the `create_object` and `update_object` generic views was removed. Use the `django.forms` package and the new `form_class` argument instead.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@8291 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-08-10 21:10:47 +00:00
|
|
|
>>> class BoundaryForm(ModelForm):
|
|
|
|
... class Meta:
|
|
|
|
... model = BoundaryModel
|
|
|
|
>>> f = BoundaryForm({'positive_integer': 100})
|
2007-10-20 13:01:40 +00:00
|
|
|
>>> f.is_valid()
|
2007-09-15 02:37:07 +00:00
|
|
|
True
|
Fixed #7830 -- Removed all of the remaining, deprecated, non-oldforms features:
* Support for representing files as strings was removed. Use `django.core.files.base.ContentFile` instead.
* Support for representing uploaded files as dictionaries was removed. Use `django.core.files.uploadedfile.SimpleUploadedFile` instead.
* The `filename`, `file_name`, `file_size`, and `chuck` properties of `UploadedFile` were removed. Use the `name`, `name`, `size`, and `chunks` properties instead, respectively.
* The `get_FIELD_filename`, `get_FIELD_url`, `get_FIELD_size`, and `save_FIELD_file` methods for Models with `FileField` fields were removed. Instead, use the `path`, `url`, and `size` attributes and `save` method on the field itself, respectively.
* The `get_FIELD_width` and `get_FIELD_height` methods for Models with `ImageField` fields were removed. Use the `width` and `height` attributes on the field itself instead.
* The dispatcher `connect`, `disconnect`, `send`, and `sendExact` functions were removed. Use the signal object's own `connect`, `disconnect`, `send`, and `send` methods instead, respectively.
* The `form_for_model` and `form_for_instance` functions were removed. Use a `ModelForm` subclass instead.
* Support for importing `django.newforms` was removed. Use `django.forms` instead.
* Support for importing `django.utils.images` was removed. Use `django.core.files.images` instead.
* Support for the `follow` argument in the `create_object` and `update_object` generic views was removed. Use the `django.forms` package and the new `form_class` argument instead.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@8291 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-08-10 21:10:47 +00:00
|
|
|
>>> f = BoundaryForm({'positive_integer': 0})
|
2007-10-20 13:01:40 +00:00
|
|
|
>>> f.is_valid()
|
2007-09-15 02:37:07 +00:00
|
|
|
True
|
Fixed #7830 -- Removed all of the remaining, deprecated, non-oldforms features:
* Support for representing files as strings was removed. Use `django.core.files.base.ContentFile` instead.
* Support for representing uploaded files as dictionaries was removed. Use `django.core.files.uploadedfile.SimpleUploadedFile` instead.
* The `filename`, `file_name`, `file_size`, and `chuck` properties of `UploadedFile` were removed. Use the `name`, `name`, `size`, and `chunks` properties instead, respectively.
* The `get_FIELD_filename`, `get_FIELD_url`, `get_FIELD_size`, and `save_FIELD_file` methods for Models with `FileField` fields were removed. Instead, use the `path`, `url`, and `size` attributes and `save` method on the field itself, respectively.
* The `get_FIELD_width` and `get_FIELD_height` methods for Models with `ImageField` fields were removed. Use the `width` and `height` attributes on the field itself instead.
* The dispatcher `connect`, `disconnect`, `send`, and `sendExact` functions were removed. Use the signal object's own `connect`, `disconnect`, `send`, and `send` methods instead, respectively.
* The `form_for_model` and `form_for_instance` functions were removed. Use a `ModelForm` subclass instead.
* Support for importing `django.newforms` was removed. Use `django.forms` instead.
* Support for importing `django.utils.images` was removed. Use `django.core.files.images` instead.
* Support for the `follow` argument in the `create_object` and `update_object` generic views was removed. Use the `django.forms` package and the new `form_class` argument instead.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@8291 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-08-10 21:10:47 +00:00
|
|
|
>>> f = BoundaryForm({'positive_integer': -100})
|
2007-10-20 13:01:40 +00:00
|
|
|
>>> f.is_valid()
|
2007-09-15 02:37:07 +00:00
|
|
|
False
|
|
|
|
|
2007-10-20 13:01:40 +00:00
|
|
|
# Formfield initial values ########
|
|
|
|
If the model has default values for some fields, they are used as the formfield
|
|
|
|
initial values.
|
Fixed #7830 -- Removed all of the remaining, deprecated, non-oldforms features:
* Support for representing files as strings was removed. Use `django.core.files.base.ContentFile` instead.
* Support for representing uploaded files as dictionaries was removed. Use `django.core.files.uploadedfile.SimpleUploadedFile` instead.
* The `filename`, `file_name`, `file_size`, and `chuck` properties of `UploadedFile` were removed. Use the `name`, `name`, `size`, and `chunks` properties instead, respectively.
* The `get_FIELD_filename`, `get_FIELD_url`, `get_FIELD_size`, and `save_FIELD_file` methods for Models with `FileField` fields were removed. Instead, use the `path`, `url`, and `size` attributes and `save` method on the field itself, respectively.
* The `get_FIELD_width` and `get_FIELD_height` methods for Models with `ImageField` fields were removed. Use the `width` and `height` attributes on the field itself instead.
* The dispatcher `connect`, `disconnect`, `send`, and `sendExact` functions were removed. Use the signal object's own `connect`, `disconnect`, `send`, and `send` methods instead, respectively.
* The `form_for_model` and `form_for_instance` functions were removed. Use a `ModelForm` subclass instead.
* Support for importing `django.newforms` was removed. Use `django.forms` instead.
* Support for importing `django.utils.images` was removed. Use `django.core.files.images` instead.
* Support for the `follow` argument in the `create_object` and `update_object` generic views was removed. Use the `django.forms` package and the new `form_class` argument instead.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@8291 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-08-10 21:10:47 +00:00
|
|
|
>>> class DefaultsForm(ModelForm):
|
|
|
|
... class Meta:
|
|
|
|
... model = Defaults
|
2007-10-20 13:01:40 +00:00
|
|
|
>>> DefaultsForm().fields['name'].initial
|
|
|
|
u'class default value'
|
2007-10-22 22:04:00 +00:00
|
|
|
>>> DefaultsForm().fields['def_date'].initial
|
2007-10-20 13:01:40 +00:00
|
|
|
datetime.date(1980, 1, 1)
|
|
|
|
>>> DefaultsForm().fields['value'].initial
|
|
|
|
42
|
2010-03-08 15:03:30 +00:00
|
|
|
>>> r1 = DefaultsForm()['callable_default'].as_widget()
|
|
|
|
>>> r2 = DefaultsForm()['callable_default'].as_widget()
|
|
|
|
>>> r1 == r2
|
|
|
|
False
|
2007-10-20 13:01:40 +00:00
|
|
|
|
Fixed #7830 -- Removed all of the remaining, deprecated, non-oldforms features:
* Support for representing files as strings was removed. Use `django.core.files.base.ContentFile` instead.
* Support for representing uploaded files as dictionaries was removed. Use `django.core.files.uploadedfile.SimpleUploadedFile` instead.
* The `filename`, `file_name`, `file_size`, and `chuck` properties of `UploadedFile` were removed. Use the `name`, `name`, `size`, and `chunks` properties instead, respectively.
* The `get_FIELD_filename`, `get_FIELD_url`, `get_FIELD_size`, and `save_FIELD_file` methods for Models with `FileField` fields were removed. Instead, use the `path`, `url`, and `size` attributes and `save` method on the field itself, respectively.
* The `get_FIELD_width` and `get_FIELD_height` methods for Models with `ImageField` fields were removed. Use the `width` and `height` attributes on the field itself instead.
* The dispatcher `connect`, `disconnect`, `send`, and `sendExact` functions were removed. Use the signal object's own `connect`, `disconnect`, `send`, and `send` methods instead, respectively.
* The `form_for_model` and `form_for_instance` functions were removed. Use a `ModelForm` subclass instead.
* Support for importing `django.newforms` was removed. Use `django.forms` instead.
* Support for importing `django.utils.images` was removed. Use `django.core.files.images` instead.
* Support for the `follow` argument in the `create_object` and `update_object` generic views was removed. Use the `django.forms` package and the new `form_class` argument instead.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@8291 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-08-10 21:10:47 +00:00
|
|
|
In a ModelForm that is passed an instance, the initial values come from the
|
|
|
|
instance's values, not the model's defaults.
|
|
|
|
>>> foo_instance = Defaults(name=u'instance value', def_date=datetime.date(1969, 4, 4), value=12)
|
|
|
|
>>> instance_form = DefaultsForm(instance=foo_instance)
|
|
|
|
>>> instance_form.initial['name']
|
2007-10-20 13:01:40 +00:00
|
|
|
u'instance value'
|
Fixed #7830 -- Removed all of the remaining, deprecated, non-oldforms features:
* Support for representing files as strings was removed. Use `django.core.files.base.ContentFile` instead.
* Support for representing uploaded files as dictionaries was removed. Use `django.core.files.uploadedfile.SimpleUploadedFile` instead.
* The `filename`, `file_name`, `file_size`, and `chuck` properties of `UploadedFile` were removed. Use the `name`, `name`, `size`, and `chunks` properties instead, respectively.
* The `get_FIELD_filename`, `get_FIELD_url`, `get_FIELD_size`, and `save_FIELD_file` methods for Models with `FileField` fields were removed. Instead, use the `path`, `url`, and `size` attributes and `save` method on the field itself, respectively.
* The `get_FIELD_width` and `get_FIELD_height` methods for Models with `ImageField` fields were removed. Use the `width` and `height` attributes on the field itself instead.
* The dispatcher `connect`, `disconnect`, `send`, and `sendExact` functions were removed. Use the signal object's own `connect`, `disconnect`, `send`, and `send` methods instead, respectively.
* The `form_for_model` and `form_for_instance` functions were removed. Use a `ModelForm` subclass instead.
* Support for importing `django.newforms` was removed. Use `django.forms` instead.
* Support for importing `django.utils.images` was removed. Use `django.core.files.images` instead.
* Support for the `follow` argument in the `create_object` and `update_object` generic views was removed. Use the `django.forms` package and the new `form_class` argument instead.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@8291 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-08-10 21:10:47 +00:00
|
|
|
>>> instance_form.initial['def_date']
|
2007-10-20 13:01:40 +00:00
|
|
|
datetime.date(1969, 4, 4)
|
Fixed #7830 -- Removed all of the remaining, deprecated, non-oldforms features:
* Support for representing files as strings was removed. Use `django.core.files.base.ContentFile` instead.
* Support for representing uploaded files as dictionaries was removed. Use `django.core.files.uploadedfile.SimpleUploadedFile` instead.
* The `filename`, `file_name`, `file_size`, and `chuck` properties of `UploadedFile` were removed. Use the `name`, `name`, `size`, and `chunks` properties instead, respectively.
* The `get_FIELD_filename`, `get_FIELD_url`, `get_FIELD_size`, and `save_FIELD_file` methods for Models with `FileField` fields were removed. Instead, use the `path`, `url`, and `size` attributes and `save` method on the field itself, respectively.
* The `get_FIELD_width` and `get_FIELD_height` methods for Models with `ImageField` fields were removed. Use the `width` and `height` attributes on the field itself instead.
* The dispatcher `connect`, `disconnect`, `send`, and `sendExact` functions were removed. Use the signal object's own `connect`, `disconnect`, `send`, and `send` methods instead, respectively.
* The `form_for_model` and `form_for_instance` functions were removed. Use a `ModelForm` subclass instead.
* Support for importing `django.newforms` was removed. Use `django.forms` instead.
* Support for importing `django.utils.images` was removed. Use `django.core.files.images` instead.
* Support for the `follow` argument in the `create_object` and `update_object` generic views was removed. Use the `django.forms` package and the new `form_class` argument instead.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@8291 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-08-10 21:10:47 +00:00
|
|
|
>>> instance_form.initial['value']
|
2007-10-20 13:01:40 +00:00
|
|
|
12
|
2009-04-16 14:26:08 +00:00
|
|
|
|
|
|
|
>>> from django.forms import CharField
|
|
|
|
>>> class ExcludingForm(ModelForm):
|
2009-05-07 18:06:22 +00:00
|
|
|
... name = CharField(max_length=255)
|
2009-04-16 14:26:08 +00:00
|
|
|
... class Meta:
|
|
|
|
... model = Defaults
|
2010-03-08 15:03:30 +00:00
|
|
|
... exclude = ['name', 'callable_default']
|
2009-04-16 14:26:08 +00:00
|
|
|
>>> f = ExcludingForm({'name': u'Hello', 'value': 99, 'def_date': datetime.date(1999, 3, 2)})
|
|
|
|
>>> f.is_valid()
|
|
|
|
True
|
|
|
|
>>> f.cleaned_data['name']
|
|
|
|
u'Hello'
|
|
|
|
>>> obj = f.save()
|
|
|
|
>>> obj.name
|
|
|
|
u'class default value'
|
|
|
|
>>> obj.value
|
|
|
|
99
|
|
|
|
>>> obj.def_date
|
|
|
|
datetime.date(1999, 3, 2)
|
2009-04-05 20:59:20 +00:00
|
|
|
>>> shutil.rmtree(temp_storage_location)
|
2009-05-10 07:44:27 +00:00
|
|
|
|
|
|
|
|
2007-10-20 13:01:40 +00:00
|
|
|
"""}
|