I\'m trying to limit the maximum amount of choices a model record can have in a ManyToManyField.
In this example there is a BlogSite that can be related to Regions. In t
Working! I have used this and its working properly. Validation required before saving the data. So you can use code in form
class BlogSiteForm(forms.ModelForm):
def clean_regions(self):
regions = self.cleaned_data['regions']
if len(regions) > 3:
raise forms.ValidationError('You can add maximum 3 regions')
return regions
class Meta:
model = BlogSite
fields = '__all__'