Enforce at least one value in a many-to-many relation, in Django?
django, orm
Solution
You can't enforce this on at the model level as @Greg details, but you can enforce it on a form by simply making the field required. This won't prevent anyone with shell-level access from manually creating a UserProfile without a foo, but it will force anyone using a browser-based form method of creation.
Problem
I have have a many-to-many relation in a Django(1.4) model. ``` class UserProfile(models.Model): foos = models.ManyToManyField(Foo) ``` I want to enforce that each User(Profile) has at least one Foo. Foos can have zero-or-more User(Profiles)s. I would love this to be enforced at the model and admin levels, but just enforcing it in the admin would be sufficient. If I understand correctly, 'many' in Django-speak is zero-or-more. I want a ManyToOneOrMore relation. How can I do this? Thanks, Chris.