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 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.
精彩评论