How to make a related field mandatory in Django?
<p>Suppose I have the following many-to-one relationship in Django:</p>

<pre><code>from django.db import models

class Person(models.Model):
    # ...

class Address(models.Model):
    # ...
    person = models.ForeignKey(Person, on_delete=models.CASCADE)
</code></pre>

<p>This allows a person to have multiple addresses.</p>

<p>I wish to make it mandatory for a person to have at least one address, so it will be impossible to save a person with no addresses in the DB.</p>

<p>How can I achieve this goal? Is it possible to make a related field mandatory (as can be done for "normal" fields using <code>blank=False</code>)?</p>

<p>As previously said, there's no way to enforce a relationship directly on the database. </p>

<p>However, you can take care of it by validating the model before saving using the <code>clean()</code> method. It will be automatically triggered on save for Django models.</p>

<pre><code>class Person(models.Model):
    .
    .
    .
    def clean(self):
        if len(self.addresses) == 0:
            raise ValidationError('At least one address is required.')
</code></pre>

python django django-models relationship