Django FileField:如何设置默认值(自动创建空文件)?

发布于 2024-09-09 16:59:03 字数 377 浏览 2 评论 0原文

我有一个这样的模型:

class MyModel(models.Model):
    name = models.CharField(max_length=255)
    code = models.FileField()

当提交一个新的 MyModel 时,我希望允许 code 字段留空,在这种情况下,我需要 Django 创建一个空文件(任意姓名)。

问题是:正确的做法是什么?

我在文档中找不到任何相关内容,因此我正在考虑手动编辑 request.FILES,然后再将其提供给 MyModelForm(),但这对我来说看起来像是一个肮脏的黑客......有什么想法吗?

谢谢。

I've got a model like this:

class MyModel(models.Model):
    name = models.CharField(max_length=255)
    code = models.FileField()

When a new MyModel is submitted, I want to allow for the code field to be left empty, in which case I need Django to create an empty file (with arbitrary name).

Question is: what is the right way to do it?

I couldn't find anything related in the docs, so I was looking at manually editing request.FILES before feeding it to MyModelForm(), but this looks like a dirty hack to me... Any ideas?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不及他 2024-09-16 16:59:03

我会将代码输入存储在 CharField 中,然后创建一个单独的函数来访问模型,如果代码不包含任何有害方法,则将其写入文件。

这负责创建文件(因为空白 CharField 将简单地输出到空文件)并允许委派给安全检查器。您的设置将如下所示:
模型:

class MyModel(models.Model):
    name = models.CharField(max_length=255)
    code = models.CharField(MAX_FILE_LENGTH)

视图:

def Submit_Code(request):
     #Create MyModel using POST data
     process_input_file(NEWLY_CREATED_MODEL_NAME)
     return HttpResponse("Upload Successful")

def process_input_file(modelName):
     #assuming unique name. Use "id=" instead if needed.
     mm = MyModel.objects.get(name=modelName)
     if passes_security_checks(mm.code):
          f = open(mm.name, "r")
          f.write(mm.code)
          f.close()

编辑
新视图:

def Submit_Code(request):
     mm = MyModel()
     mm.name = request.POST.get('name')
     f = open(mm.name,"r")
     f.write(request.POST.get('code')
     f.close()
     #then associate the newly created file with the FileField however you want
     #passing through authentication/checking if need be.
     return HttpResponse("Upload Successful")

I would store the code input in a CharField and then create a separate function that accesses the model and if the code does not contain any harmful methods it is then written to a file.

This takes care of creating the file (as a blank CharField will simply be outputted to an empty file) and allows for delegation to a security checker. Your setup would then look something like the following:
Model:

class MyModel(models.Model):
    name = models.CharField(max_length=255)
    code = models.CharField(MAX_FILE_LENGTH)

View:

def Submit_Code(request):
     #Create MyModel using POST data
     process_input_file(NEWLY_CREATED_MODEL_NAME)
     return HttpResponse("Upload Successful")

def process_input_file(modelName):
     #assuming unique name. Use "id=" instead if needed.
     mm = MyModel.objects.get(name=modelName)
     if passes_security_checks(mm.code):
          f = open(mm.name, "r")
          f.write(mm.code)
          f.close()

Edit
New view:

def Submit_Code(request):
     mm = MyModel()
     mm.name = request.POST.get('name')
     f = open(mm.name,"r")
     f.write(request.POST.get('code')
     f.close()
     #then associate the newly created file with the FileField however you want
     #passing through authentication/checking if need be.
     return HttpResponse("Upload Successful")
[浮城] 2024-09-16 16:59:03

只需将字段设置为允许空和空白即可。这将使该字段成为可选字段。

code = models.FileField(null=True, blank=True)

Just set the field to allow null and blank. This will make the field optional.

code = models.FileField(null=True, blank=True)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文