I've got a simple Django model for a Group that has a list of Contacts. Each group must have either a Primary contact ForeignKey
or a All contacts BooleanField
selected but not both and not none.
class Group(models.Model):
contacts = models.ManyToManyField(Contact)
contact_primary = models.ForeignKey(Contact, related_name="contact_primary", null=True)
all_contacts = models.BooleanField(null=True)
How can I ensure that:
The model can't be saved unless either
contact_primary
orall_contacts
(but not both) is set. I guess that would be by implementing theGroup.save()
method? Or should it be theGroup.clean()
method??In Django Admin either disable the other field when one is selected or at least provide a meaningful error message if both or none are set and the admin tries to save it?
Thanks!