如何将文件分配给 Django 中的 FileField?
我尝试:
from django.core.files import File
...
...
att_path = os.path.join(detach_dir, filename)
if not os.path.isfile(att_path):
fp = open(att_path, 'wb')
djangofile = File(fp)
fp.write(part.get_payload(decode=True))
user = User.objects.get(id=1)
files = File.objects.create(src=djangofile, user=user)
fp.close()
但我有这个错误:
AttributeError:类型对象“文件”没有属性“对象”
模型.py:
class File(models.Model):
user = models.ForeignKey(User)
src = models.FileField(upload_to="src")