2012-09-17 18 views
0

限制表单集选择如何限制在Django表单集的选择为5。 我的意见最大数量:使用Django

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

什么是您的模板,视图? – Rohan

回答

0

max_num与表单集功能,

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

这是我的猜测,通常该formset需要max_num为kwarg。试一试!

+2

我试过了。但选择不限。 – Raji