使用djang限制表单集选择

2024-09-30 01:25:13 发布

您现在位置:Python中文网/ 问答频道 /正文

如何将django formset中的选项限制为最多5个。 我的观点是:

file_attachment_formset = get_audioattachment_formset(FileAttachmentForm, extra=1, can_delete=True)

我的表格为:

^{pr2}$

在模型.py公司名称:

class FileAttachment(models.Model):
    post          = models.ForeignKey(Post, related_name = 'file_attachments')
    picture       = models.ImageField(upload_to = 'uploads/picture/', null = True, blank = True)
    audio_video   = models.URLField(null = True, blank = True, verbose_name = "Audio/Video URL", verify_exists = True)

在视图.py公司名称:

file_attachment_formset = get_audioattachment_formset(FileAttachmentForm, extra=1, can_delete=True, max_num=3)
if request.method == 'POST':
   postForm = MyPostForm(request.POST, instance = post)
   formset = file_attachment_formset(request.POST, request.FILES, instance = post)
   if formset.is_valid():
                #FileAttachment.objects.filter(post = newpost).delete()
                formset = file_attachment_formset(request.POST, request.FILES, instance = newpost)
                formset.save()
   else:
       formset = file_attachment_formset(instance = post)  

帮助我如何将图片的选择限制在最多5个?在


Tags: instancetrueattachmentgetmodelsrequestdeletepost

热门问题