```
class Test(TestCase):
def test_post(self):
img = BytesIO(IMAGE)
data = {
'image': img,
}
response = self.client.post(url, data=data)
```
The view takes the data and fill the following ModelForm that fails
validation:
```
class Image(models.Model):
image = models.ImageField(_(u'Immagine'), upload_to='image')
class ImageForm(forms.ModelForm):
class Meta:
model = Image
```
As a workaround, adding a name attribute to the BytesIO object make the
code work again:
```
img.name = 'ciao.jpg'
```
--
Ticket URL: <https://code.djangoproject.com/ticket/28148>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
Old description:
> The following test code snippet stopped working on django 1.11 raising a
> validation error because the posted image has not a valid file extension:
>
> ```
> class Test(TestCase):
> def test_post(self):
> img = BytesIO(IMAGE)
> data = {
> 'image': img,
> }
> response = self.client.post(url, data=data)
> ```
>
> The view takes the data and fill the following ModelForm that fails
> validation:
>
> ```
> class Image(models.Model):
> image = models.ImageField(_(u'Immagine'), upload_to='image')
>
> class ImageForm(forms.ModelForm):
> class Meta:
> model = Image
> ```
>
> As a workaround, adding a name attribute to the BytesIO object make the
> code work again:
>
> ```
> img.name = 'ciao.jpg'
> ```
New description:
The following test code snippet stopped working on django 1.11 raising a
validation error because the posted image has not a valid file extension:
{{{
class Test(TestCase):
def test_post(self):
img = BytesIO(IMAGE)
data = {
'image': img,
}
response = self.client.post(url, data=data)
}}}
The view takes the data and fill the following ModelForm that fails
validation:
{{{
class Image(models.Model):
image = models.ImageField(_(u'Immagine'), upload_to='image')
class ImageForm(forms.ModelForm):
class Meta:
model = Image
}}}
As a workaround, adding a name attribute to the BytesIO object make the
code work again:
{{{
img.name = 'ciao.jpg'
}}}
--
--
Ticket URL: <https://code.djangoproject.com/ticket/28148#comment:1>
Old description:
> The following test code snippet stopped working on django 1.11 raising a
> validation error because the posted image has not a valid file extension:
>
> {{{
> class Test(TestCase):
> def test_post(self):
> img = BytesIO(IMAGE)
> data = {
> 'image': img,
> }
> response = self.client.post(url, data=data)
> }}}
>
> The view takes the data and fill the following ModelForm that fails
> validation:
>
> {{{
> class Image(models.Model):
> image = models.ImageField(_(u'Immagine'), upload_to='image')
>
> class ImageForm(forms.ModelForm):
> class Meta:
> model = Image
> }}}
>
> As a workaround, adding a name attribute to the BytesIO object make the
> code work again:
>
> {{{
> img.name = 'ciao.jpg'
> }}}
New description:
The following test code snippet stopped working after upgrading django to
1.11 from 1.10.x. The code now raises a validation error because the
posted image has not a valid file extension:
{{{
class Test(TestCase):
def test_post(self):
img = BytesIO(IMAGE)
data = {
'image': img,
}
response = self.client.post(url, data=data)
}}}
The view takes the data and fill the following ModelForm that fails
validation:
{{{
class Image(models.Model):
image = models.ImageField(_(u'Immagine'), upload_to='image')
class ImageForm(forms.ModelForm):
class Meta:
model = Image
}}}
As a workaround, adding a name attribute to the BytesIO object make the
code work again:
{{{
img.name = 'ciao.jpg'
}}}
--
--
Ticket URL: <https://code.djangoproject.com/ticket/28148#comment:2>
Comment (by Tim Graham):
We could document the backwards incompatible change. Did you have anything
else in mind?
--
Ticket URL: <https://code.djangoproject.com/ticket/28148#comment:3>
Comment (by rm_):
Replying to [comment:3 Tim Graham]:
> We could document the backwards incompatible change. Did you have
anything else in mind?
I think a mention of ImageFields name attribute requirements should be
added in the paragraph that suggests to use StringIO / BytesIO here:
https://docs.djangoproject.com/en/1.11/topics/testing/tools/#django.test.Client.post
--
Ticket URL: <https://code.djangoproject.com/ticket/28148#comment:4>
* type: Uncategorized => Cleanup/optimization
* component: Forms => Documentation
* easy: 0 => 1
* stage: Unreviewed => Accepted
--
Ticket URL: <https://code.djangoproject.com/ticket/28148#comment:5>
* has_patch: 0 => 1
Comment:
PR here https://github.com/django/django/pull/8466
--
Ticket URL: <https://code.djangoproject.com/ticket/28148#comment:6>
* status: new => closed
* resolution: => fixed
Comment:
In [changeset:"bdf192c59357a0d8117f6f34c94fb32a51e7a774" bdf192c5]:
{{{
#!CommitTicketReference repository=""
revision="bdf192c59357a0d8117f6f34c94fb32a51e7a774"
Fixed #28148 -- Doc'd ImageField name validation concerns with the test
client.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/28148#comment:7>
Comment (by Tim Graham <timograham@…>):
In [changeset:"643413f654d52ca4d7bf6d9402ff9ca24d2ea635" 643413f6]:
{{{
#!CommitTicketReference repository=""
revision="643413f654d52ca4d7bf6d9402ff9ca24d2ea635"
[1.11.x] Fixed #28148 -- Doc'd ImageField name validation concerns with
the test client.
Backport of bdf192c59357a0d8117f6f34c94fb32a51e7a774 from master
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/28148#comment:8>