Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django (1.2) Forms: ManyToManyField Help Text

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)
like image 565
Zach Avatar asked Jul 13 '10 22:07

Zach


People also ask

What is Manytomanyfield in Django?

A ManyToMany field is used when a model needs to reference multiple instances of another model. Use cases include: A user needs to assign multiple categories to a blog post. A user wants to add multiple blog posts to a publication.

How do you make a field optional in Django?

In order to make a field optional, we have to say so explicitly. If we want to make the pub_time field optional, we add blank=True to the model, which tells Django's field validation that pub_time can be empty.

What is ModelForm in Django?

Django Model Form It is a class which is used to create an HTML form by using the Model. It is an efficient way to create a form without writing HTML code. Django automatically does it for us to reduce the application development time.


2 Answers

class Item(models.Model):
    ...
    category = models.ManyToManyField(Category, null=True,blank=True)
    category.help_text = ''
    ...
like image 53
Raul Avatar answered Sep 30 '22 16:09

Raul


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

like image 31
laffuste Avatar answered Sep 30 '22 17:09

laffuste