Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting label_suffix for a Django model formset

Tags:

python

django

I have a Product model that I use to create ProductFormSet. How do I specify the label_suffix to be something other than the default colon? I want it to be blank. Solutions I've seen only seem to apply when initiating a form - here.

ProductFormSet = modelformset_factory(Product, exclude=('abc',))    
products = Product.objects.order_by('product_name')
pformset = ProductFormSet(queryset=products)
like image 915
user3062149 Avatar asked Oct 18 '22 02:10

user3062149


1 Answers

In Django 1.9+, you can use the form_kwargs option.

ProductFormSet = modelformset_factory(Product, exclude=('abc',))    
products = Product.objects.order_by('product_name')
pformset = ProductFormSet(queryset=products, form_kwargs={'label_suffix': ''})

In earlier Django versions, you could define a ProductForm class that sets the label_suffix to blank in the __init__ method, and then pass that form class to modelformset_factory.

class ProductForm(forms.ModelForm):
    ...
    def __init__(self, *args, **kwargs):
        super(ProductForm, self).__init__(*args, **kwargs)
        self.label_suffix = ''

ProductFormSet = modelformset_factory(Product, form=ProductForm, exclude=('abc',))    
like image 154
Alasdair Avatar answered Nov 03 '22 22:11

Alasdair