2๐
โ
You can achieve it by using jquery, this would save all the serverside overhead;
in your model admin add this:
class Media:
js = ('PATHA_AFTER_STATIC/limitchoice.js', )
Put your limitchoice.js in the statics/ folder (credit to: https://stackoverflow.com/a/2046293/288387)
$("id_MODELNAME_to").on("change", "option", function () {
if ( 3 <= $(this).siblings(":selected").length ) {
$(this).removeAttr("selected");
alert("Only 3 choices allowed");
}
});โโโโโโโโโโ
Note the id of your manytomany field id_MODELNAME_to wich points to the second box.
Hope it helps
๐คelsadek
Source:stackexchange.com