如何从Django表单集中编辑(删除)条目

时间:2019-02-20 03:21:24

标签: django python-3.x django-models django-forms django-views

简介:我有一篇帖子,其中包含多个图片,我可以借助2个模型来实现此目的。发布和准备。帖子模型具有usertitlemessagepost_image

class Post(models.Model):
    user = models.ForeignKey(User, related_name='posts')
    title = models.CharField(max_length=250, unique=True)    
    message = models.TextField() 
    post_image = models.ImageField(upload_to='post_images/')

我正在使用另一种模型来获取称为Prep的其他图像。该图像可以具有多个图像。

class Prep (models.Model): #(Images)
    post = models.ForeignKey(Post, on_delete=models.CASCADE, related_name='post_prep')
    image = models.ImageField(upload_to='post_prep_images/', blank=True, null=True)
    image_title = models.CharField(max_length=100)
    image_description = models.CharField(max_length=250)

但是与之前不同,准备模型中的每个图像都有一个image_title和一个image_description

问题:我能够成功创建帖子,并且几乎可以编辑该帖子,但 一个例外 我无法减少图片。

  • 如果我有3张Prep图片,我可以添加更多图片,直到允许的最大数量
  • 我可以编辑现有图像

      
        
    • 我无法减少准备图像的数量
    •   

    我收到表格错误 This field is required.

如何解决此错误

我的post_edit视图

def post_edit(request, slug):
    post = get_object_or_404(Post, slug=slug)
    ImageFormSet = modelformset_factory(Prep, fields=('image', 'image_title', 'image_description'), extra=7, max_num=7,
                                        min_num=2)
    if post.user != request.user:
        raise Http404()
    if request.method == "POST":
        form = PostEditForm(request.POST or None, request.FILES or None, instance=post)
        formset = ImageFormSet(request.POST or None, request.FILES or None)

        if form.is_valid() and formset.is_valid():
            form.save()
            data = Prep.objects.filter(post=post)
            for index, f in enumerate(formset):
                if f.cleaned_data:
                    if f.cleaned_data['id'] is None:
                        photo = Prep(post=post, image=f.cleaned_data.get('image'), image_title=f.cleaned_data.get('image_title'),
                                     image_description=f.cleaned_data.get('image_description'))
                        photo.save()

                   #I thought the below code will do the trick but it doesn't seem to be    
                    elif f.cleaned_data['image'] is False or f.cleaned_data['image_title'] is False or f.cleaned_data['image_description'] is False:
                        photo = Prep.objects.get(id=data[index].id)
                        photo.image.delete()
                        photo.image_title.delete()
                        photo.image_description.delete()
                        photo.id.delete()    

                    else:
                        photo = Prep(post=post, image=f.cleaned_data.get('image'), image_title=f.cleaned_data.get('image_title'),
                                     image_description=f.cleaned_data.get('image_description'))
                        d = Prep.objects.get(id=data[index].id)
                        d.image=photo.image
                        d.image_title=photo.image_title
                        d.image_description=photo.image_description
                        d.save()

            return HttpResponseRedirect(post.get_absolute_url())
    else:
        form = PostEditForm(instance=post)
        formset = ImageFormSet(queryset=Prep.objects.filter(post=post))
    context = {'form': form, 'post': post, 'formset': formset}
    return render(request, 'posts/post_edit.html', context)

1 个答案:

答案 0 :(得分:0)

Open your workspace file, then File --> WorkSpace Settings In shared Workspace settings, choose Build System: Legacy Build System. Then run ionic cordova run -l can_delete=True,