Django: Add field to model formset
I can I add fields to a model formset? It seems you can add fields if you user normal formset but not with model formsets (at least it's not the same way). I don't think I should use inline formset either ..?
I want to let users edit their photoalbum (django-photologue). So far I've manage to do this:
PhotoFormSet = modelformset_factory(Photo,
                                       exclude=(
                                        'effect',
     开发者_开发问答                                   'caption',
                                        'title_slug',
                                        'crop_from',
                                        'is_public',
                                        'slug',
                                        'tags'
                                       ))
context['gallery_form'] = PhotoFormSet(queryset=self.object.gallery.photos.all())
The problem is that I have to add a checkbox for each photo saying "Delete this photo" and a radio select saying "Set this to album cover".
Thanks in advance!
You can add fields. Just define a form in the normal way, then tell modelformset_factory to use that as the basis for the formset:
MyPhotoForm(forms.ModelForm):
    delete_box = forms.BooleanField()
    class Meta:
        model = Photo
        exclude=('effect',
                 'caption',
                 'title_slug',
                 'crop_from',
                 'is_public',
                 'slug',
                 'tags'
                ))
PhotoFormSet = modelformset_factory(Photo, form=MyPhotoForm)
 
         加载中,请稍侯......
 加载中,请稍侯......
      
精彩评论