开发者

Django: Blank Choices in Many To Many Fields

开发者 https://www.devze.com 2022-12-10 03:59 出处:网络
When making forms in Django, the IntegerField comes with a blank choice (a bunch of dashes \"------\") if called with blank=True and null=True.Is there any way to get ManyToManyField to include such a

When making forms in Django, the IntegerField comes with a blank choice (a bunch of dashes "------") if called with blank=True and null=True. Is there any way to get ManyToManyField to include such an explicit blank choice?

I've tried subclassing ManyToM开发者_JS百科anyField with no success:

class ManyFieldWithBlank(ManyToManyField):  

    """  
    A Many-to-Many Field with a blank choice  
    """  
    def get_choices_default(self):
        return Field.get_choices(self, include_blank=True)


That is not really an improvement on the interface, IMO.

Why not have a button in your template saying "none of these" or "reset choices"? Better yet - if your field is called "Blah" make the button say "Unselect all Blah".

The button would just have some javascript to clear out any selection in the select box.

This is a much clearer UI for the user and easy to implement.

Disclaimer: IANADesigner.

0

精彩评论

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