Skip to content

Instantly share code, notes, and snippets.

@aliceridgway
Created June 5, 2022 20:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aliceridgway/5fcd03824cdd59697e4f23b281622a33 to your computer and use it in GitHub Desktop.
Save aliceridgway/5fcd03824cdd59697e4f23b281622a33 to your computer and use it in GitHub Desktop.
from django.db import models
from django.contrib import auth
USER = auth.get_user_model()
class UserProfile(models.Model):
user = models.OneToOneField(to=USER, related_name="profile", on_delete=models.PROTECT)
first_name = models.CharField(max_length=255)
last_name = models.CharField(max_length=255)
bio = models.TextField()
def __str__(self):
return f"{self.first_name} {self.last_name}"
class Tag(models.Model):
name = models.CharField(max_length=255)
def __str__(self):
return f"{self.name}"
class Post(models.Model):
title = models.CharField(max_length=255)
body = models.TextField()
author = models.ForeignKey(to=UserProfile, on_delete=models.CASCADE, related_name="posts")
tags = models.ManyToManyField(to=Tag, related_name="posts", blank=True)
def __str__(self):
return f"{self.title} by {self.author.first_name} {self.author.last_name}"
class Comment(models.Model):
post = models.ForeignKey(to=Post, on_delete=models.CASCADE, related_name="comments")
body = models.TextField()
author = models.ForeignKey(to=UserProfile, on_delete=models.CASCADE, related_name="comments")
def __str__(self):
return f"Comment by {self.author.first_name} {self.author.last_name} on {self.post.title}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment