如何将django formset中的选项限制为最大数量5。 我的观点为:
file_attachment_formset = get_audioattachment_formset(FileAttachmentForm, extra=1, can_delete=True)
我的表格为:
class FileAttachmentForm(forms.ModelForm):
class Meta:
model = FileAttachment
def __init__(self, *args, **kw):
super(forms.ModelForm, self).__init__(*args, **kw)
self.fields['audio_video'].widget.attrs['class'] = 'form-text'
def get_audioattachment_formset(form, formset = models.BaseInlineFormSet, **kwargs):
return models.inlineformset_factory(Post, FileAttachment, form, formset, **kwargs)
Models.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)
views.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?
答案 0 :(得分:0)
使用formset函数传递max_num
file_attachment_formset = get_audioattachment_formset(FileAttachmentForm, extra=1, can_delete=True, max_num=5)
这是我的猜测,通常formset将max_num
作为kwarg。试一试!