user203749
user203749

Reputation:

Django: Blank Choices in Many To Many Fields

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 ManyToManyField 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)

Upvotes: 0

Views: 953

Answers (1)

cethegeek
cethegeek

Reputation: 6394

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.

Upvotes: 4

Related Questions