Django

Code

root/django/trunk/tests/regressiontests/forms/models.py

Revision 8291, 2.6 kB (checked in by gwilson, 4 weeks ago)

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.
  • Property svn:eol-style set to native
Line 
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 """}
Note: See TracBrowser for help on using the browser.