Trying to figure out what the relationship between my post topic and post like would be

Hi,

I’m trying to figure out what the relationship between my post topic and post like would be. This is my models code thus far:

f

rom django.db import models
from django.contrib.auth.models import User

# Create your models here.

class Board(models.Model):
  name = models.CharField(max_length=30, unique=True)
  description = models.CharField(max_length=100)

  def __str__(self):
    return self.name

class Topic(models.Model):
  subject = models.CharField(max_length=255)
  last_updated = models.DateTimeField(auto_now_add=True)
  board = models.ForeignKey(Board, on_delete=models.CASCADE, related_name='topics')
  starter = models.ForeignKey(User, on_delete=models.CASCADE, related_name='topics')
  
  def __str__(self):
    return self.subject

class Post(models.Model):
  message = models.TextField(max_length=4000)
  topic = models.ForeignKey(Topic, on_delete=models.CASCADE, related_name='posts')
  created_at = models.DateTimeField(auto_now_add=True)
  updated_at = models.DateTimeField(null=True)
  created_by = models.ForeignKey(User, on_delete=models.CASCADE, related_name='posts')
  updated_by = models.ForeignKey(User, on_delete=models.CASCADE, null=True, related_name='+')
  post_liked_by = models.ManyToManyField(User, default=None, blank=True)

  def __str__(self):
    return self.message

  @property
  def post_num_likes(self):
    return self.post_liked.all().count()

POST_LIKE_CHOICES = (
  ('♥️', '♥️'),
  ('♡', '♡'),
)

class PostLike(models.Model):
  user = models.ForeignKey(User, on_delete=models.CASCADE)
  post = models.ForeignKey(Post, on_delete=models.CASCADE)
  post_like_value = models.CharField(choices=POST_LIKE_CHOICES, default='♥️', max_length=2)

  def __str__(self):
    return str(self.post)

I get the relationship between the board and topic, topic and post, and user and topic and user and post. But what exactly is the relationship between the post like and the user and how would I represent it on a UML class diagram? I can share the digram as it is right now if needed. Thanks!

1 Like

I’m a little confused by your description here. In the first part of your post:

I’m having difficulty reconciling this with the description after the code:

Are these two separate questions? Or are those questions related somehow?

It’s the same question. But I found the answer. The only thing at this point in the development of my project that I need to use to retrieve the PostLike is by its id/pk. And I thereby was able to update any UML diagrams to reflect that. Thanks for responding!

The relationship between your models for post likes and users is already defined correctly using a ManyToManyField . Here’s a breakdown:

  • Model: PostLike
  • Relationship with User: This is a Many-to-Many relationship. One user can like many posts, and one post can be liked by many users. This is reflected by the ManyToManyField on the user field in the PostLike model.
  • Relationship with Post: Similar to the user, this is also a Many-to-Many relationship. A PostLike instance connects a specific user to a specific post they liked. This is reflected by the ForeignKey on the post field in the PostLike model.
classDiagram
  User <-->|likes| PostLike
  PostLike -->|liked by| Post
end classDiagram

In this diagram:

  • Rectangles represent classes (models) with their names inside.
  • Lines represent relationships between classes.
  • Crow’s feet (<--> ) represent a Many-to-Many relationship.
  • An arrow (-> ) represents a One-to-Many or ForeignKey relationship. Here, it shows a PostLike instance belongs to a single User and a single Post .
  • Labels on the lines describe the relationship (“likes” and “liked by”).
1 Like

@WarnerSmith Thanks so much for this explanation! That is what I did in my updated UML diagram. Here is the post where I add it: https://www.mariadcampbell.com/blog/how-to-create-a-fullstack-application-using-django-and-python-part-10
But perhaps I could expand upon the ManyToManyField a bit. Thanks!

1 Like

@WarnerSmith, I credit you there along with the link to your response!

1 Like

@warnersmith the completed update to the post I linked is now published. I just had to fix a typo, add a section to Table of Contents (Footnote), and updated lastModified.