如何解决FILEFIELD 属性没有与之关联的文件
当我尝试使用图像字段更新表单时,此错误不断出现。我正在尝试上传或更新客户 logo 图片。
当我尝试上传图片时,包含图片的字段显示“[field] 属性没有与之关联的文件”
我正在使用模型表单和通用更新视图。我不知道如何使该属性具有与之关联的文件。
我在模型中尝试了 save() 覆盖,但它仍然给了我那个错误。
models.py
class Customers(models.Model):
associations = models.ManyToManyField(Association)
company_name = models.CharField(help_text='Company Name',max_length=200,default=None)
contact1_first_name = models.CharField(help_text='Primary Contact First name',max_length=50,default=None)
contact1_last_name = models.CharField(help_text='Primary Contact Last name',default=None)
address1 = models.CharField(help_text='Address 1',default=None)
address2 = models.CharField(help_text='Address 2',default=None,blank=True)
city = models.CharField(help_text='City',default=None)
state = models.CharField(help_text='State',default=None)
zip_code = models.CharField(help_text='Postal/ZIP',max_length=20,default=None)
phone = models.CharField(help_text='Phone',default=None)
email = models.EmailField(default=None)
contact2_first_name = models.CharField(max_length=50,null=True,blank=True)
contact2_last_name = models.CharField(max_length=50,blank=True)
contact2_phone = models.CharField(help_text='Phone',blank=True)
contact2_email = models.EmailField(default=None,blank=True)
fax = models.CharField(max_length=20,blank=True)
mobile = models.CharField(max_length=50,blank=True,null=True)
web_address = models.CharField(max_length=50,blank=True)
date_entered = models.DateTimeField(auto_Now_add=True)
is_active = models.BooleanField(default=True)
date_turned_off = models.DateTimeField(default=None,blank=True)
num_locs = models.SmallIntegerField(default=1)
notes = models.TextField(help_text='Notes',default=None)
vertical_cat = models.IntegerField(default=0)
custom_cat = models.IntegerField(default=0)
logo = models.FileField(upload_to='media/cust-logs/',blank=True)
max_scripts = models.IntegerField(default=0)
branch_custom_scripts = models.BooleanField(default=False)
receive_emails = models.BooleanField(default=True)
customer_rep_id = models.SmallIntegerField(default=0,null=True)
customer_writer_id = models.SmallIntegerField(default=0,null=True)
customer_male_vt_id = models.SmallIntegerField(default=0,null=True)
customer_fem_vt = models.SmallIntegerField(default=0,null=True)
customer_sales_rep = models.SmallIntegerField(default=0,null=True)
contract_date_first = models.DateTimeField(default=None,blank=True)
contract_date_renewal = models.DateTimeField(default=None,blank=True)
contract_billing_type = models.CharField(max_length=200,blank=True)
anniversary = models.DateTimeField(default=None,blank=True)
is_on_contract = models.BooleanField(default=False)
internal_flag = models.IntegerField(default=0,null=True)
shuff_freq_pref = models.SmallIntegerField(default=0,blank=True)
shuff_freq_upd = models.DateTimeField(default=None,blank=True)
shuff_freq_7 = models.BooleanField(default=True)
shuff_freq_14 = models.BooleanField(default=True)
shuff_freq_30 = models.BooleanField(default=True)
shuff_freq_60 = models.BooleanField(default=True)
shuff_freq_90 = models.BooleanField(default=True)
shuff_freq_180 = models.BooleanField(default=False)
shuff_walk_thru = models.DateTimeField(default=None,blank=True)
shuff_walk_thru_by = models.ForeignKey(Emp,related_name='+',on_delete=models.CASCADE)
cust_classification = models.ForeignKey(CustomerClasses,on_delete=models.CASCADE)
date_updated = models.DateTimeField(auto_Now=True,blank=True)
updated_by = models.SmallIntegerField(default=0)
audit_note = models.TextField(default=None,blank=True)
audit_exception = models.BooleanField(default=False)
audit_date = models.DateTimeField(auto_Now_add=True)
script_exception = models.BooleanField(default=False)
script_exception_date = models.DateTimeField(default=None,blank=True)
reports_exception = models.BooleanField(default=False)
reports_exception_date = models.DateTimeField(default=None,blank=True)
using_script_credits = models.BooleanField(default=False)
def __str__(self):
return self.company_name
def save(self):
super().save()
img = Image.open(self.logo.path)
if img.height > 300 or img.width > 300:
output_size = (300,300)
img.thumbnail(output_size)
img.save(self.logo.image)
我把它改成上传到 S3,记录中有一个图片路径,上面写着“这个后端不支持绝对路径。”
views.py
class CustomerUpdateView(LoginrequiredMixin,UpdateView):
model = Customers
template_name = 'customers/customer_form.html'
fields = 'company_name','contact1_first_name','contact1_last_name','contact2_first_name',\
'contact2_last_name','contact2_email','contact2_phone','address1','address2',\
'city','state','zip_code','phone','email','mobile','web_address','notes',\
'cust_classification','logo'
success_url = 'customers:cust-home'
def form_valid(self,form):
return super().form_valid(form)
def save(self):
user = super(CustomerForm,self).save()
x = self.cleaned_data.get('x')
y = self.cleaned_data.get('y')
w = self.cleaned_data.get('w')
h = self.cleaned_data.get('h')
image = Image.open(user.primaryphoto)
cropped_image = image.crop(x,y,w + x,h,+ y)
resized_image = cropped_image.resize((200,200),Image.ANTIALIAS)
fh = storage.open(user.primaryphoto.name,"wb")
picture_format = 'png'
resized_image.save(fh,picture_format)
fh.close()
return user
我从另一个 StackOverflow 帖子中借用了上面的一些 save() 部分。
这是我的客户表单
forms.py
class CustomerForm(forms.ModelForm):
class Meta:
model = Customers
fields = 'company_name',\
'contact1_last_name',\
'contact2_phone',\
'mobile','shuff_walk_thru_by','logo','associations'
我今天的 Django 项目只是一团糟。它非常庞大,我已经研究了 2 个月,哈哈。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。