Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save eclecticmiraclecat/f43f0fa019ae4e5955a7334155c843fe to your computer and use it in GitHub Desktop.
Save eclecticmiraclecat/f43f0fa019ae4e5955a7334155c843fe to your computer and use it in GitHub Desktop.

one to one

  • one person one license
# models.py
class Person(models.Model):
  name = models.CharField(max_length=20)

class License(models.Model):
  license_number = models.IntegerField()
  person = models.OneToOneField(Person, on_delete=models.CASCADE)
>>> from cbvApp.models import Person, License
>>> p = Person(name='james')
>>> p.save()
>>> l = License(person=p, license_number='112233')
>>> l.save()
>>> p.license
Person
+----+-------+
| id | name  |
+----+-------+
|  1 | james |
+----+-------+

License
+----+----------------+-----------+
| id | license_number | person_id |
+----+----------------+-----------+
|  1 |         112233 |         1 |
+----+----------------+-----------+
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment