我有一个类似的问题: how to manual assign imagefield in Django
除了我的文件来自远程主机并且我只得到一个 url,所以类似于:
class Book(models.Model):
name = models.CharField()
url = models.URLField()
file = models.FileField(null=True, blank=True, editable=False)
class BookForm(forms.ModelForm):
class Meta:
model = Book
def view(request):
if request.POST:
form = BookForm()
if form.is_valid():
request = form.instance.source_url
try:
f = urlopen(request)
# ...create open file obj...
form.instance.local_file.save(form.instance.ref, f.read(), True)
except Exception:
print 3
在这种情况下我应该如何f
变成一个打开的文件对象,以便我可以保存它的内容?