无法访问UpdateView中的ModelForm图像字段以更新Django中的图像已解决

问题描述

我正在尝试为一个产品更新多个图像。如果存在的话,我已经清除了图像,但是我无法访问从ModelForm传递来的新图像。

我的模特:

def get_image_path(instance,filename):
    name = instance.title
    return f'{name}/{filename}'

def get_file_path(instance,filename):
    name = instance.title
    return f'{name}/files/{filename}'

def get_upload_path(instance,filename):
    name = instance.product.title
    return f'{name}/images/{filename}'

class Productimage(models.Model):
    product = models.ForeignKey('Product',on_delete=models.CASCADE)
    image = models.ImageField(upload_to=get_upload_path,blank=True,null=True)

    def __str__(self):
        return self.product.title

class Product(models.Model):
    title = models.CharField(max_length= 100)
    price = models.DecimalField(decimal_places=2,max_digits=9,default=0.00)
    description = models.TextField()
    image = models.ImageField(upload_to=get_image_path,null=True)
    video = models.FileField(upload_to=get_file_path,validators=[validate_file_extension],null=True)
    discount_price = models.DecimalField(decimal_places=2,max_digits=8,default=0.00)
    date_created = models.DateTimeField(auto_Now_add=True)
    date_updated = models.DateTimeField(auto_Now=True)

    slug = models.SlugField(max_length=100,unique=True)

    supplier = models.ForeignKey(USER,on_delete=models.SET_NULL,null=True)

    season_choice = models.CharField(choices=SEASON_CHOICES,max_length=10,default='summer')
    tags = models.ManyToManyField(Tag,related_name='products')
    

    def __str__(self):     
        return self.title

我的模型形式:

class ProductCreateForm(forms.ModelForm):
    more_images = forms.FileField(required=False,widget=forms.FileInput(attrs={
        "class": "form-control","multiple": True
    }))

    class Meta:
        model = Product
        fields = ['title','price','description','image','video','discount_price','season_choice','tags']

我的UpdateView

class ProductUpdateView(LoginrequiredMixin,UserPassesTestMixin,UpdateView):
    model = Product
    form_class = ProductCreateForm
    template_name = 'product/product_update.html'
    success_url = reverse_lazy('home')

   
    def form_valid(self,form):
        form.instance.supplier = self.request.user               

        product = self.get_object() 
        
        # Clear existing images/This is working
        for post_image in Productimage.objects.filter(product=product):
            post_image.delete()

        # Updating new images/This is not working
        images = self.request.FILES.getlist('more_images')
        print(images)
        
        for i in images:
            Productimage.objects.create(product=product,image=i)

        form.save()
        return super().form_valid(form)

如何从CBV中form_valid()方法内的ModelForm访问ImageField中传递的图像。我的代码给出了空的查询集和空的列表。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...