aptwebapps
aptwebapps

Reputation: 1876

What is the correct way to validate ManyToManyFields?

If I have a model with a ManyToManyField and I want to restrict it to instances that have a particular property, what's the best way to do this? I can do it in the form validation or in the view, but I'd like to do it closer to the model.

For example, how can I only allow instances of class B that have is_cool set to True to be associated with class A instances?

from django.db import models

class A(models.Model):
    cool_bees = models.models.ManyToManyField('B') 

class B(models.Model):
    is_cool = models.BooleanField(default=False)

Upvotes: 1

Views: 343

Answers (1)

Tisho
Tisho

Reputation: 8482

To be closer to the model, you can use the m2m_changed signal to check if the model matches your requirements, so the code could look like this:

import django.db.models.signals

def validate(sender, instance, action, reverse, model, pk_set, **kwargs):
    if action == "pre_add":
        # if we're adding some A's that're not cool - a.cool_bees.add(b)
        if not reverse and model.objects.filter(pk__in=pk_set, is_cool=False):
              raise ValidationError("You're adding an B that is not cool")
        # or if we add using the reverse - b.A_set.add(a)
        if reverse and not instance.is_cool:
              raise ValidationError("You cannot insert this non-cool B to A")


signals.m2m_changed.connect(validate, sender=A.cool_bees.through)

Upvotes: 3

Related Questions