Django (1.2) 表单:ManyToManyField 帮助文本

8 投票
4 回答
3058 浏览
提问于 2025-04-16 01:15

我希望我理解错了,但在我看来,要让一个 ManyToManyField 没有 help_text,唯一的方法就是为表单写一个 __init__ 方法,并且覆盖 self.fields[fieldname].help_text。这真的是唯一的方法吗?我更喜欢使用 CheckboxSelectMultiple 这个控件,那我是不是每次使用 ManyToManyField 的表单都得定义一个 __init__ 方法呢?

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)

4 个回答

0

你说得没错。我自己也遇到过这个问题,所以我自己做了一个ManyToManyField来解决它。

这里有一个相关的bug,我在上面留言过:http://code.djangoproject.com/ticket/6183

3

在一个普通的表单中:

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

如果你想要更改这个(国际化)字符串:

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

在django 1.6中测试过

13
class Item(models.Model):
    ...
    category = models.ManyToManyField(Category, null=True,blank=True)
    category.help_text = ''
    ...

当然可以!请把你想要翻译的内容发给我,我会帮你用简单易懂的语言解释清楚。

撰写回答