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

**URL:** <https://forum.djangoproject.com/t/trying-to-figure-out-what-the-relationship-between-my-post-topic-and-post-like-would-be/34614>\
**Category:** Using the ORM\
**Created:** [September 8, 2024, 10:25pm UTC](https://forum.djangoproject.com/t/trying-to-figure-out-what-the-relationship-between-my-post-topic-and-post-like-would-be/34614 "2024-09-08T22:25:06Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![interglobalmedia](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/interglobalmedia/32/23637_2.png) [@interglobalmedia](https://forum.djangoproject.com/u/interglobalmedia)\
**Post date:** [September 8, 2024, 10:25pm UTC](https://forum.djangoproject.com/t/trying-to-figure-out-what-the-relationship-between-my-post-topic-and-post-like-would-be/34614/1 "2024-09-08T22:25:07Z")

</div>

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

```auto
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!

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [September 8, 2024, 10:40pm UTC](https://forum.djangoproject.com/t/trying-to-figure-out-what-the-relationship-between-my-post-topic-and-post-like-would-be/34614/2 "2024-09-08T22:40:11Z")

</div>

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

> [@interglobalmedia](#):
>
> what the relationship between my post topic and post like would be.

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

> [@interglobalmedia](#):
>
> But what exactly is the relationship between the post like and the user

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

---

<div class="post-metadata">

**Author:** ![interglobalmedia](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/interglobalmedia/32/23637_2.png) [@interglobalmedia](https://forum.djangoproject.com/u/interglobalmedia)\
**Post date:** [September 9, 2024, 3:52am UTC](https://forum.djangoproject.com/t/trying-to-figure-out-what-the-relationship-between-my-post-topic-and-post-like-would-be/34614/3 "2024-09-09T03:52:39Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![warnersmith](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/warnersmith/32/23772_2.png) [@warnersmith](https://forum.djangoproject.com/u/warnersmith)\
**Post date:** [September 9, 2024, 11:00am UTC](https://forum.djangoproject.com/t/trying-to-figure-out-what-the-relationship-between-my-post-topic-and-post-like-would-be/34614/4 "2024-09-09T11:00:19Z")

</div>

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.

```auto
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”).

---

<div class="post-metadata">

**Author:** ![interglobalmedia](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/interglobalmedia/32/23637_2.png) [@interglobalmedia](https://forum.djangoproject.com/u/interglobalmedia)\
**Post date:** [September 10, 2024, 9:56am UTC](https://forum.djangoproject.com/t/trying-to-figure-out-what-the-relationship-between-my-post-topic-and-post-like-would-be/34614/5 "2024-09-10T09:56:29Z")

</div>

@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](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!

---

<div class="post-metadata">

**Author:** ![interglobalmedia](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/interglobalmedia/32/23637_2.png) [@interglobalmedia](https://forum.djangoproject.com/u/interglobalmedia)\
**Post date:** [September 10, 2024, 10:17am UTC](https://forum.djangoproject.com/t/trying-to-figure-out-what-the-relationship-between-my-post-topic-and-post-like-would-be/34614/6 "2024-09-10T10:17:29Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![interglobalmedia](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/interglobalmedia/32/23637_2.png) [@interglobalmedia](https://forum.djangoproject.com/u/interglobalmedia)\
**Post date:** [September 10, 2024, 10:36am UTC](https://forum.djangoproject.com/t/trying-to-figure-out-what-the-relationship-between-my-post-topic-and-post-like-would-be/34614/7 "2024-09-10T10:36:42Z")

</div>

@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.
