我在django应用中有一个postmodel和一个postform,但是如果我尝试从表单中删除postcover,则会出现以下错误:
“ postcover”属性没有与之关联的文件。
我不明白为什么会这样,因为对于附加后字段,它运行良好。
models.py
class Post(models.Model):
author = models.ForeignKey(User, on_delete=models.CASCADE)
title = models.CharField(verbose_name="Post Title", max_length=40)
content = models.TextField(verbose_name="Post Content", max_length=5000)
tag = models.CharField(verbose_name="Tags/Meta - (sep. by comma)", max_length=50, blank=True)
category = models.ForeignKey(Category, verbose_name="Category", on_delete=models.CASCADE, null=True)
postattachment = fields.FileField(
verbose_name="Post Attachment",
blank=True,
null=True,
upload_to=get_file_path_user_uploads,
validators=[file_extension_postattachment, file_size_postattachment]
)
postcover = fields.ImageField(
null=True,
blank=True,
upload_to=get_file_path_user_uploads,
validators=[default_image_size, default_image_file_extension],
dependencies=[FileDependency(processor=ImageProcessor(
format='PNG', quality=99, scale={'max_width': 700, 'max_height': 700}))])
up_vote = models.IntegerField(verbose_name='Post Up-Vote(s)', default=0)
down_vote = models.IntegerField(verbose_name='Post Down-Vote(s)', default=0)
published_date = models.DateField(auto_now_add=True, null=True)
forms.py
class PostForm(forms.ModelForm):
class Meta:
model = Post
widgets = {
'title': forms.TextInput(attrs={'placeholder': 'e.g.: Something about Mr. Nobody'}),
'tag': forms.TextInput(attrs={
'placeholder': 'e.g.: April, Cars, Montana'}),
}
fields = ['title',
'category',
'content',
'tag',
'postcover',
'postattachment'
]
captcha = CaptchaField()
field_order = ['title',
'category',
'content',
'tag',
'postcover',
'postattachment',
'captcha'
]
def __init__(self, *args, **kwargs):
kwargs.setdefault('label_suffix', '')
super(PostForm, self).__init__(*args, **kwargs)
self.fields['title'].widget.attrs.update({'class': 'blablablablaaaa'})
...