gpt4 book ai didi

Django FileField 未保存到 upload_to 位置

转载 作者:行者123 更新时间:2023-12-02 20:33:54 25 4
gpt4 key购买 nike

我有一个 Attachment 模型,该模型在 Django 1.4.1 应用程序中具有 FileField 。此 FileField 有一个可调用的 upload_to 参数,根据 Django docs应该在保存表单(以及模型)时调用。

当我运行下面的 FormTest 时,永远不会调用 upload_to 可调用函数,因此该文件不会出现在 upload_to< 提供的位置中 方法。我做错了什么?

请注意,在 ModelTest(也在下面)中通过测试时,upload_to 方法按预期工作。

我看过的事情不是似乎是问题所在:

  • Attachment 模型中属性的顺序。我发现一些问题似乎表明这个顺序很重要。由于我的 upload_to 方法使用 directory 属性,我认为他的方法可能会导致问题。它不是。无论如何,这不会导致该方法不被调用。
  • 也许 is_valid() 没有被调用。不,我已经确认是这样。
  • ...

测试:

from core.forms.attachments import AttachmentForm
from django.test import TestCase
import unittest
from django.core.files.uploadedfile import SimpleUploadedFile
from django.core.files.storage import default_storage

def suite():
return unittest.TestSuite(
[
unittest.TestLoader().loadTestsFromTestCase(FormTest),
]
)

class FormTest(TestCase):
def test_form_1(self):
filename = 'filename'
f = file(filename)
data = {'name':'name',}
file_data = {'attachment_file':SimpleUploadedFile(f.name,f.read()),}
form = AttachmentForm(data=data,files=file_data)
self.assertTrue(form.is_valid())
attachment = form.save()
root_directory = 'attachments'
upload_location = root_directory + '/' + attachment.directory + '/' + filename
self.assertTrue(attachment.attachment_file) # Fails
self.assertTrue(default_storage.exists(upload_location)) # Fails

附件模型:

from django.db import models
from parent_mixins import Parent_Mixin
import uuid
from django.db.models.signals import pre_delete,pre_save
from dirtyfields import DirtyFieldsMixin

def upload_to(instance,filename):
return 'attachments/' + instance.directory + '/' + filename

def uuid_directory_name():
return uuid.uuid4().hex

class Attachment(DirtyFieldsMixin,Parent_Mixin,models.Model):
attachment_file = models.FileField(blank=True,null=True,upload_to=upload_to)
directory = models.CharField(blank=False,default=uuid_directory_name,null=False,max_length=32)
name = models.CharField(blank=False,default=None,null=False,max_length=128)

class Meta:
app_label = 'core'

def __str__(self):
return unicode(self).encode('utf-8')

def __unicode__(self):
return unicode(self.name)

@models.permalink
def get_absolute_url(self):
return('core_attachments_update',(),{'pk': self.pk})

# def save(self,*args,**kwargs):
# super(Attachment,self).save(*args,**kwargs)

def pre_delete_callback(sender, instance, *args, **kwargs):
if not isinstance(instance, Attachment): return
if not instance.attachment_file: return
instance.attachment_file.delete(save=False)

def pre_save_callback(sender, instance, *args, **kwargs):
if not isinstance(instance, Attachment): return
if not instance.attachment_file: return
if instance.is_dirty():
dirty_fields = instance.get_dirty_fields()
if 'attachment_file' in dirty_fields:
old_attachment_file = dirty_fields['attachment_file']
old_attachment_file.delete()

pre_delete.connect(pre_delete_callback)
pre_save.connect(pre_save_callback)

附件表格:

from ..models.attachments import Attachment
from crispy_forms.helper import FormHelper
from crispy_forms.layout import Div,Layout,HTML,Field,Fieldset,Button,ButtonHolder,Submit
from django import forms

class AttachmentFormHelper(FormHelper):
form_tag=False

layout = Layout(
Div(
Div(
Field('name',css_class='span4'),
Field('attachment_file',css_class='span4'),
css_class='span4',
),
css_class='row',
),
)

class AttachmentForm(forms.ModelForm):
helper = AttachmentFormHelper()

class Meta:
fields=('attachment_file','name')
model = Attachment

class AttachmentInlineFormHelper(FormHelper):
form_tag=False
form_style='inline'

layout = Layout(
Div(
Div(
Field('name',css_class='span4'),
Field('attachment_file',css_class='span4'),
Field('DELETE',css_class='span4'),
css_class='span4',
),
css_class='row',
),
)

class AttachmentInlineForm(forms.ModelForm):
helper = AttachmentInlineFormHelper()

class Meta:
fields=('attachment_file','name')
model = Attachment

更新

我还使用这些单元测试对 Attachment 模型类进行测试 - 全部通过:

from core.models.attachments import Attachment
from core.models.attachments import upload_to
from django.test import TestCase
import unittest
from django.core.files.storage import default_storage
from django.core.files.base import ContentFile

def suite():
return unittest.TestSuite(
[
unittest.TestLoader().loadTestsFromTestCase(ModelTest),
]
)

class ModelTest(TestCase):
def test_model_minimum_fields(self):
attachment = Attachment(name='name')
attachment.attachment_file.save('test.txt',ContentFile("hello world"))
attachment.save()
self.assertEqual(str(attachment),'name')
self.assertEqual(unicode(attachment),'name')
self.assertTrue(attachment.directory)

# def test_model_full_fields(self):
# attachment = Attachment()
# attachement.save()

def test_file_operations_basic(self):
root_directory = 'attachments'
filename = 'test.txt'
attachment = Attachment(name='name')
attachment.attachment_file.save(filename,ContentFile('test'))
attachment.save()
upload_location = root_directory + '/' + attachment.directory + '/' + filename
self.assertEqual(upload_to(attachment,filename),upload_location)
self.assertTrue(default_storage.exists(upload_location))

def test_file_operations_delete(self):
root_directory = 'attachments'
filename = 'test.txt'
attachment = Attachment(name='name')
attachment.attachment_file.save(filename,ContentFile('test'))
attachment.save()
upload_location = upload_to(attachment,filename)
attachment.delete()
self.assertFalse(default_storage.exists(upload_location))

def test_file_operations_change(self):
root_directory = 'attachments'
filename_1 = 'test_1.txt'
attachment = Attachment(name='name')
attachment.attachment_file.save(filename_1,ContentFile('test'))
attachment.save()
upload_location_1 = upload_to(attachment,filename_1)
self.assertTrue(default_storage.exists(upload_location_1))

filename_2 = 'test_2.txt'
attachment.attachment_file.save(filename_2,ContentFile('test'))
attachment.save()
upload_location_2 = upload_to(attachment,filename_2)
self.assertTrue(default_storage.exists(upload_location_2))
self.assertFalse(default_storage.exists(upload_location_1))

最佳答案

如果我将 Attachment 模型的 attachment_file 字段更改为不允许空白或空值,则测试可以通过。

具体改变

attachment_file = models.FileField(blank=True,null=True,upload_to=upload_to)

attachment_file = models.FileField(blank=False,null=False,upload_to=upload_to)

我不知道为什么这会对我的测试产生影响,并且希望得到 Django 专家的解释。如果这些参数不为 False,为什么 FileField 不会保存文件?

关于Django FileField 未保存到 upload_to 位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13040580/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com