skip to Main Content

How can I create a field that is different for every user. For example if we have an instance of a Comment model and there is a field that is different for every user that tells if user has liked that comment. So when I map through the objects i can just check if a user has liked it. Something like a local field for every user.

2

Answers


  1. I’m not sure if I understand your question correctly, but are you looking to create a UDL? Do you mean an object that stores information for analytical purposes? If so, you can simply create an empty object and add any data you wish to it using an Event Listener.

    Login or Signup to reply.
  2. To track which user has liked a comment, you can create a related entity.

    class Comment(models.Model):
      # The text of the comment
      text = models.CharField(max_length=1024)
      # The post the comment relates to
      post = models.ForeignKey(Post, on_delete=models.CASCADE)
      # The user who made the comment
      user = models.ForeignKey(User, on_delete=models.CASCADE)
    
    class CommentLike(models.Model):
      # Indication of like. Could be something more complex
      # such as an enum or another entity.
      liked = models.BooleanField(default=False)
      # The user who changed the like status
      user = models.ForeignKey(User, on_delete=models.CASCADE)
      # The liked/unliked comment
      comment = models.ForeignKey(
        Comment, 
        on_delete=models.CASCADEm,
        related_name="likes",
      )
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search