Skip to content

Instantly share code, notes, and snippets.

@mchesler613
Last active February 28, 2021 03:43
Show Gist options
  • Save mchesler613/d7a15eab07f15015249bb0298903bb39 to your computer and use it in GitHub Desktop.
Save mchesler613/d7a15eab07f15015249bb0298903bb39 to your computer and use it in GitHub Desktop.
Django Models
# Person has one-to-one relationship with Email and Bio
class Person(models.Model):
STATUS = [
('AWAY', 'I am away'),
('BUSY', 'I am busy'),
('MEETING', 'I am in a meeting'),
('IDLE', 'I am available')
]
name = models.CharField(max_length=20)
status = models.CharField(max_length=10, choices=STATUS, default='IDLE')
age = models.IntegerField(default=0)
...
# Bio has a one-to-one relationship with Person, but can be NULL
class Bio(models.Model):
bio = models.TextField()
image = models.URLField()
person = models.OneToOneField(Person, on_delete=models.CASCADE)
...
# Email has one-to-one relationship with Person
class Email(models.Model):
address = models.EmailField()
person = models.OneToOneField(Person, on_delete=models.CASCADE)
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment