import os
import tempfile
from django.test import override_settings, TestCase, Client
TEMP_PATH = tempfile.mkdtemp(
dir=os.path.join(BASE_DIR, 'tests/tmp'))
class TestSomething(TestCase)
def setUp(self):
...
def tearDown(self):
TEMP_PATH.clean_up()
...
@override_settings(MEDIA_URL=TEMP_PATH)
def test_article_sorting(self):
...
# или
def test_article_sorting(self):
with override_settings(MEDIA_URL=self.image_path):
...
import tempfile
from django.test import override_settings
...
def setUp(self):
self.temp_path = tempfile.TemporaryDirectory(
dir=os.path.join(BASE_DIR, 'tests/tmp'))
def tearDown(self):
self.temp_path.clean_up()
...
@override_settings(MEDIA_URL=self.temp_path.name)
def test_article_sorting(self):
...
if instance.pk:
try:
article = Article.objects.get(pk=instance.pk)
if instance.image and article.image != instance.image:
slide.image.delete(False)
except Article.DoesNotExist:
pass
requests.post(a['upload_url'], files={'photo': file.getvalue()})
import requests
from io import BytesIO
r = requests.get('http://i.stack.imgur.com/bq6O5.png')
f = BytesIO(r.content)