from django.db import models
class User(models.Model):
username = models.CharField(max_length=255, unique=True)
password = models.CharField(max_length=128)
profile_pic_url = models.CharField(max_length=255, blank=True)
authored_posts = models.ForeignKey('Post', on_delete=models.CASCADE, related_name='authors', null=True, blank=True)
primary_comment_on_post = models.ForeignKey('Comment', on_delete=models.CASCADE, related_name='primary_commenter', null=True, blank=True)
authored_replies = models.ForeignKey('Reply', on_delete=models.CASCADE, related_name='repliers', null=True, blank=True)
comments = models.ForeignKey('Comment', on_delete=models.CASCADE, related_name='commenters')
likes = models.ForeignKey('Post', on_delete=models.CASCADE, related_name='liked_by_users')
created_at = models.DateTimeField(auto_now_add=True)
def __str__(self):
return self.username
class Post(models.Model):
image_url = models.CharField(max_length=255)
description = models.TextField()
author = models.ForeignKey(User, on_delete=models.CASCADE, related_name='posts')
comments = models.ForeignKey('Comment', on_delete=models.CASCADE, related_name='posted_on')
likes = models.ForeignKey(User, on_delete=models.CASCADE, related_name='liked_posts')
created_at = models.DateTimeField(auto_now_add=True)
def __str__(self):
return f"Post: {self.description[:20]}"
class Comment(models.Model):
description = models.TextField()
post = models.ForeignKey(Post, on_delete=models.CASCADE, related_name='post_comments')
author = models.ForeignKey(User, on_delete=models.CASCADE, related_name='authored_comments')
replies = models.ForeignKey('Reply', on_delete=models.CASCADE, related_name='commented_on')
likes = models.ForeignKey(User, on_delete=models.CASCADE, related_name='liked_comments')
created_at = models.DateTimeField(auto_now_add=True)
def __str__(self):
return f"Comment on Post {self.post.id}: {self.description[:20]}"
class Reply(models.Model):
description = models.TextField()
comment = models.ForeignKey(Comment, on_delete=models.CASCADE, related_name='comment_replies')
author = models.ForeignKey(User, on_delete=models.CASCADE, related_name='replies')
likes = models.ForeignKey(User, on_delete=models.CASCADE, related_name='liked_replies')
created_at = models.DateTimeField(auto_now_add=True)
def __str__(self):
return f"Reply on Comment {self.comment.id}: {self.description[:20]}"