Django (1.2) Forms: ManyToManyField Help Text
Asked Answered
P

4

8

I hope I'm wrong, but it looks to me like the only way to have no help_text for a ManyToManyField is write an __init__ method for the form and overwrite self.fields[fieldname].help_text. Is that really the only way? I prefer to use CheckboxSelectMultple widgets, so am I really going to have to define an __init__ method for any form that uses a ManyToManyField?

class ManyToManyField(RelatedField, Field):
    description = _("Many-to-many relationship")
    def __init__(self, to, **kwargs):
        #some other stuff
        msg = _('Hold down "Control", or "Command" on a Mac, to select more than one.')
        self.help_text = string_concat(self.help_text, ' ', msg)
Photoluminescence answered 13/7, 2010 at 22:50 Comment(0)
C
13
class Item(models.Model):
    ...
    category = models.ManyToManyField(Category, null=True,blank=True)
    category.help_text = ''
    ...
Carbineer answered 25/12, 2010 at 16:26 Comment(1)
This is still the way to go as of 1.5 it seems, although I believe this is fixed for 1.6.Smithery
B
3

In a regular form:

MyForm.base_fields['many_to_many_field'].help_text = ''

If you want to change the (i18n) string:

class MyForm(forms.ModelForm):
    def __init__(self, *args, **kwargs):
        super(MyForm, self).__init__( *args, **kwargs)
        self.base_fields['many_to_many_field'].help_text = _('Choose at least one stuff') # or nothing

Tested with django 1.6

Buhl answered 11/8, 2014 at 6:7 Comment(0)
A
0

You are not wrong. I ran into this problem myself and I did create my own ManyToManyField in order to get around this.

Here is a related bug that I commented on: http://code.djangoproject.com/ticket/6183

Accra answered 13/7, 2010 at 23:31 Comment(0)
A
0

you can also do it in your Admin class by overriding get_form:

class FooAdmin(ModelAdmin):
    ...
    def get_form(self, request, obj=None, **kwargs):
        form = ModelAdmin.get_form(self, request, obj=obj, **kwargs)
        form.base_fields['bar'].widget = CheckboxSelectMultiple()
        form.base_fields['bar'].help_text = ''
        return form
Audiophile answered 19/4, 2013 at 14:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.