mirror of
https://github.com/django/django.git
synced 2025-10-31 09:41:08 +00:00
queryset-refactor: Merged from trunk up to [7025].
git-svn-id: http://code.djangoproject.com/svn/django/branches/queryset-refactor@7029 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
@@ -14,7 +14,7 @@ from django.db import models
|
||||
|
||||
class Category(models.Model):
|
||||
name = models.CharField(max_length=20)
|
||||
parent = models.ForeignKey('self', null=True, related_name='child_set')
|
||||
parent = models.ForeignKey('self', blank=True, null=True, related_name='child_set')
|
||||
|
||||
def __unicode__(self):
|
||||
return self.name
|
||||
|
||||
@@ -7,6 +7,9 @@ examples are probably a poor fit for the ModelForm syntax. In other words,
|
||||
most of these tests should be rewritten.
|
||||
"""
|
||||
|
||||
import os
|
||||
import tempfile
|
||||
|
||||
from django.db import models
|
||||
|
||||
ARTICLE_STATUS = (
|
||||
@@ -55,6 +58,20 @@ class PhoneNumber(models.Model):
|
||||
def __unicode__(self):
|
||||
return self.phone
|
||||
|
||||
class TextFile(models.Model):
|
||||
description = models.CharField(max_length=20)
|
||||
file = models.FileField(upload_to=tempfile.gettempdir())
|
||||
|
||||
def __unicode__(self):
|
||||
return self.description
|
||||
|
||||
class ImageFile(models.Model):
|
||||
description = models.CharField(max_length=20)
|
||||
image = models.FileField(upload_to=tempfile.gettempdir())
|
||||
|
||||
def __unicode__(self):
|
||||
return self.description
|
||||
|
||||
__test__ = {'API_TESTS': """
|
||||
>>> from django import newforms as forms
|
||||
>>> from django.newforms.models import ModelForm
|
||||
@@ -701,4 +718,142 @@ ValidationError: [u'Select a valid choice. 4 is not one of the available choices
|
||||
True
|
||||
>>> f.cleaned_data
|
||||
{'phone': u'312-555-1212', 'description': u'Assistance'}
|
||||
|
||||
# FileField ###################################################################
|
||||
|
||||
>>> class TextFileForm(ModelForm):
|
||||
... class Meta:
|
||||
... model = TextFile
|
||||
|
||||
# Test conditions when files is either not given or empty.
|
||||
|
||||
>>> f = TextFileForm(data={'description': u'Assistance'})
|
||||
>>> f.is_valid()
|
||||
False
|
||||
>>> f = TextFileForm(data={'description': u'Assistance'}, files={})
|
||||
>>> f.is_valid()
|
||||
False
|
||||
|
||||
# Upload a file and ensure it all works as expected.
|
||||
|
||||
>>> f = TextFileForm(data={'description': u'Assistance'}, files={'file': {'filename': 'test1.txt', 'content': 'hello world'}})
|
||||
>>> f.is_valid()
|
||||
True
|
||||
>>> type(f.cleaned_data['file'])
|
||||
<class 'django.newforms.fields.UploadedFile'>
|
||||
>>> instance = f.save()
|
||||
>>> instance.file
|
||||
u'.../test1.txt'
|
||||
|
||||
# Edit an instance that already has the file defined in the model. This will not
|
||||
# save the file again, but leave it exactly as it is.
|
||||
|
||||
>>> f = TextFileForm(data={'description': u'Assistance'}, instance=instance)
|
||||
>>> f.is_valid()
|
||||
True
|
||||
>>> f.cleaned_data['file']
|
||||
u'.../test1.txt'
|
||||
>>> instance = f.save()
|
||||
>>> instance.file
|
||||
u'.../test1.txt'
|
||||
|
||||
# Delete the current file since this is not done by Django.
|
||||
|
||||
>>> os.unlink(instance.get_file_filename())
|
||||
|
||||
# Override the file by uploading a new one.
|
||||
|
||||
>>> f = TextFileForm(data={'description': u'Assistance'}, files={'file': {'filename': 'test2.txt', 'content': 'hello world'}}, instance=instance)
|
||||
>>> f.is_valid()
|
||||
True
|
||||
>>> instance = f.save()
|
||||
>>> instance.file
|
||||
u'.../test2.txt'
|
||||
|
||||
>>> instance.delete()
|
||||
|
||||
# Test the non-required FileField
|
||||
|
||||
>>> f = TextFileForm(data={'description': u'Assistance'})
|
||||
>>> f.fields['file'].required = False
|
||||
>>> f.is_valid()
|
||||
True
|
||||
>>> instance = f.save()
|
||||
>>> instance.file
|
||||
''
|
||||
|
||||
>>> f = TextFileForm(data={'description': u'Assistance'}, files={'file': {'filename': 'test3.txt', 'content': 'hello world'}}, instance=instance)
|
||||
>>> f.is_valid()
|
||||
True
|
||||
>>> instance = f.save()
|
||||
>>> instance.file
|
||||
u'.../test3.txt'
|
||||
>>> instance.delete()
|
||||
|
||||
# ImageField ###################################################################
|
||||
|
||||
# ImageField and FileField are nearly identical, but they differ slighty when
|
||||
# it comes to validation. This specifically tests that #6302 is fixed for
|
||||
# both file fields and image fields.
|
||||
|
||||
>>> class ImageFileForm(ModelForm):
|
||||
... class Meta:
|
||||
... model = ImageFile
|
||||
|
||||
>>> image_data = open(os.path.join(os.path.dirname(__file__), "test.png")).read()
|
||||
|
||||
>>> f = ImageFileForm(data={'description': u'An image'}, files={'image': {'filename': 'test.png', 'content': image_data}})
|
||||
>>> f.is_valid()
|
||||
True
|
||||
>>> type(f.cleaned_data['image'])
|
||||
<class 'django.newforms.fields.UploadedFile'>
|
||||
>>> instance = f.save()
|
||||
>>> instance.image
|
||||
u'.../test.png'
|
||||
|
||||
# Edit an instance that already has the image defined in the model. This will not
|
||||
# save the image again, but leave it exactly as it is.
|
||||
|
||||
>>> f = ImageFileForm(data={'description': u'Look, it changed'}, instance=instance)
|
||||
>>> f.is_valid()
|
||||
True
|
||||
>>> f.cleaned_data['image']
|
||||
u'.../test.png'
|
||||
>>> instance = f.save()
|
||||
>>> instance.image
|
||||
u'.../test.png'
|
||||
|
||||
# Delete the current image since this is not done by Django.
|
||||
|
||||
>>> os.unlink(instance.get_image_filename())
|
||||
|
||||
# Override the file by uploading a new one.
|
||||
|
||||
>>> f = ImageFileForm(data={'description': u'Changed it'}, files={'image': {'filename': 'test2.png', 'content': image_data}}, instance=instance)
|
||||
>>> f.is_valid()
|
||||
True
|
||||
>>> instance = f.save()
|
||||
>>> instance.image
|
||||
u'.../test2.png'
|
||||
|
||||
>>> instance.delete()
|
||||
|
||||
# Test the non-required ImageField
|
||||
|
||||
>>> f = ImageFileForm(data={'description': u'Test'})
|
||||
>>> f.fields['image'].required = False
|
||||
>>> f.is_valid()
|
||||
True
|
||||
>>> instance = f.save()
|
||||
>>> instance.image
|
||||
''
|
||||
|
||||
>>> f = ImageFileForm(data={'description': u'And a final one'}, files={'image': {'filename': 'test3.png', 'content': image_data}}, instance=instance)
|
||||
>>> f.is_valid()
|
||||
True
|
||||
>>> instance = f.save()
|
||||
>>> instance.image
|
||||
u'.../test3.png'
|
||||
>>> instance.delete()
|
||||
|
||||
"""}
|
||||
|
||||
BIN
tests/modeltests/model_forms/test.png
Normal file
BIN
tests/modeltests/model_forms/test.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 482 B |
@@ -749,32 +749,59 @@ Traceback (most recent call last):
|
||||
...
|
||||
ValidationError: [u'This field is required.']
|
||||
|
||||
>>> f.clean('', '')
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
ValidationError: [u'This field is required.']
|
||||
|
||||
>>> f.clean('', 'files/test1.pdf')
|
||||
'files/test1.pdf'
|
||||
|
||||
>>> f.clean(None)
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
ValidationError: [u'This field is required.']
|
||||
|
||||
>>> f.clean(None, '')
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
ValidationError: [u'This field is required.']
|
||||
|
||||
>>> f.clean(None, 'files/test2.pdf')
|
||||
'files/test2.pdf'
|
||||
|
||||
>>> f.clean({})
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
ValidationError: [u'No file was submitted.']
|
||||
|
||||
>>> f.clean({}, '')
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
ValidationError: [u'No file was submitted.']
|
||||
|
||||
>>> f.clean({}, 'files/test3.pdf')
|
||||
'files/test3.pdf'
|
||||
|
||||
>>> f.clean('some content that is not a file')
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
ValidationError: [u'No file was submitted. Check the encoding type on the form.']
|
||||
|
||||
>>> f.clean({'filename': 'name', 'content':None})
|
||||
>>> f.clean({'filename': 'name', 'content': None})
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
ValidationError: [u'The submitted file is empty.']
|
||||
|
||||
>>> f.clean({'filename': 'name', 'content':''})
|
||||
>>> f.clean({'filename': 'name', 'content': ''})
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
ValidationError: [u'The submitted file is empty.']
|
||||
|
||||
>>> type(f.clean({'filename': 'name', 'content':'Some File Content'}))
|
||||
>>> type(f.clean({'filename': 'name', 'content': 'Some File Content'}))
|
||||
<class 'django.newforms.fields.UploadedFile'>
|
||||
|
||||
>>> type(f.clean({'filename': 'name', 'content': 'Some File Content'}, 'files/test4.pdf'))
|
||||
<class 'django.newforms.fields.UploadedFile'>
|
||||
|
||||
# URLField ##################################################################
|
||||
|
||||
Reference in New Issue
Block a user