Limit Maximum Choices of ManyToManyField

后端 未结 3 1381
清酒与你
清酒与你 2021-02-20 16:51

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

3条回答
  •  野的像风
    2021-02-20 17:34

    You can override clean method on your BlogSite model

    from django.core.exceptions import ValidationError
    
    class BlogSite(models.Model):
    
        blog_owner = models.ForeignKey(User)
        site_name = models.CharField(max_length=300)
        regions = models.ManyToManyField('Region', blank=True, null=True)
    
        def clean(self, *args, **kwargs):
            if self.regions.count() > 3:
                raise ValidationError("You can't assign more than three regions")
            super(BlogSite, self).clean(*args, **kwargs)
            #This will not work cause m2m fields are saved after the model is saved
    

    And if you use django's ModelForm then this error will appear in form's non_field_errors.

    EDIT:

    M2m fields are saved after the model is saved, so the code above will not work, the correct way you can use m2m_changed signal:

    from django.db.models.signals import m2m_changed
    from django.core.exceptions import ValidationError
    
    
    def regions_changed(sender, **kwargs):
        if kwargs['instance'].regions.count() > 3:
            raise ValidationError("You can't assign more than three regions")
    
    
    m2m_changed.connect(regions_changed, sender=BlogSite.regions.through)
    

    Give it a try it worked for me.

提交回复
热议问题