开发者

Django: Add field to model formset

开发者 https://www.devze.com 2023-02-05 13:49 出处:网络
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

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)
0

精彩评论

暂无评论...
验证码 换一张
取 消