+ 7

[Semi-Solved] How would I go about making an effective chained comment feature?

I am working on a project that mimics a social network. I am having trouble with coming up with a way to make comments that can be chained, for example, have a comment that has it's own set of comments. My initial thinking was to have the Comment model take a reference of it's self but I am not sure if that would be such a great practice. And even if I were to do that, would that not mean when I called the API from javascript for the comments that I'd have to keep making a bunch of calls to get a specific comment and it's children and/or parent? I have linked the current model for my comment and I'm definitely open to any changes to it. https://code.sololearn.com/cRns9xB739Vd/?ref=app

20th Apr 2023, 2:58 PM
Justice
Justice - avatar
36 Réponses
+ 6
This kind of feature is actually out of my grasp currently when it comes to the level I am in computer science wise. As The Cereal Killer brought out, an implementation would require a data structure called Trees which I have not done enough research on and practice with. And if anyone would like to see a free course that would go over them: https://www.coursera.org/learn/algorithms-graphs-data-structures Beware, it has a lot of prerequisites though. Even myself won't be getting to it for a while but it's definitely getting done. I'll just mark this answer as complete so it'll be at the top and be sure to look at the resources TCK gave as well.
20th Apr 2023, 3:48 PM
Justice
Justice - avatar
+ 5
The Cereal Killer Thank you so much, I really appreciate it! I was actually hoping you'd see my question LOL Definitely going to take a look at that book.
20th Apr 2023, 3:49 PM
Justice
Justice - avatar
+ 4
The Cereal Killer Just as you posted this answer, I was doing some research and came across that package but it's not maintained so I'm little worried about using it. I'll still give it a deeper look though. And I'm in the middle of seeing which course in OSSU has a better explaination of trees as well so I can get a better understanding of that before I implement it blindly. And NoSQL has been something that has piqued my interest but for now, I wanna stick with Django since I am still quite a beginner in web development as a whole and don't wanna switch around to fast. But that's definitely good to know!
20th Apr 2023, 3:33 PM
Justice
Justice - avatar
+ 4
Not to me, haha. It was a force of habit but I'll take it out just incase you aren't to others LOL
20th Apr 2023, 4:02 PM
Justice
Justice - avatar
+ 2
Mirielle you're right. I deleted my post after posting it realizing it was probably not right, even before you replied. My bad. I was thinking of another thing....😅 I was thinking trees and traversing them to quickly find stuff. But I see that it is not really about traversing but more of a data model problem and how to daisy chain comments for easy access. Maybe something like adding comment = models.ForeignKey('self', on_delete=models.CASCADE, related_name"comment_reply") to the Comment class? Then comments can have comments. This could just be a link that the user may or may not click to display the comment_reply. Of course you might have to put a limit on how deep it goes. you reply reminded me of this.🫣 https://news.ycombinator.com/item?id=25025061 ok. graphql is not a magic bullet. Point noted. You win...😅
22nd Apr 2023, 8:42 AM
Bob_Li
Bob_Li - avatar
+ 2
Bob_Li I got a suggestion to do similar from one of the director's at OSSU to also create a nullable parent, but I am still not understanding how I would also effectively call that API in my JS to display it properly, that I'm just going to hold off. I know it's going to require some recurrsion and stuff but I've really rather not touch it till I get a proper grasp on the subject. I'm an overpreparer for stuff and even this entire project is just to prepare me for another personal project that I super-duper want to work on LOL
22nd Apr 2023, 10:36 AM
Justice
Justice - avatar
+ 1
Justice how about doing a simple mockup to see how it works? Too bad you can't do Django in Sololearn, but a simple Django project shouldn't be too hard to set up. to create links, maybe something like this https://stackoverflow.com/questions/69601233/how-can-i-link-a-comment-to-its-corresponding-post-in-django or maybe a slug field using unique_for_date for urls.
22nd Apr 2023, 11:27 AM
Bob_Li
Bob_Li - avatar
+ 1
Mirielle Perfect questions especially since I'm doing a notifications feature.
26th Apr 2023, 12:00 PM
Justice
Justice - avatar
+ 1
Mirielle Justice If you think about it you do get a tree like structure class Comment(models.Model): ... # comment fields parent = models.ForeignKey('self', on_delete=models.CASCADE, related_name='children') When someone replies to your comment they take your comment as a parent node w = Comment(..., parent=H) We could define our simple notification model like so class Notification(models.Model): field_id = models.PositiveIntegerField() message = models.TextField() we call our notification like so n = Notification(field_id=w.id, message="Someone replied to your comment") The notification contains comment (W)s id so if we wanted to direct our users straight to comment W we could create a route thag takes a comment id as a parameter eg https://example.com/post/comments/id
26th Apr 2023, 12:12 PM
Anonymous
Anonymous - avatar
+ 1
Mirielle The ForeignKey is a many-to-one relationship, so there will be branching and thus a treelike structure, I suppose.
26th Apr 2023, 12:35 PM
Bob_Li
Bob_Li - avatar
+ 1
Mirielle I get what you are saying, lets forget about django and databases for now and view it in this manner - A post is created - Comment W is added - 4 people A B C and D reply to comment W directly considering these 4 people can all reference W as their parent so we have a dynamic structure with no limit as to how many child nodes a parent can have struct Comment { nodes{} } w = { ... # other fields nodes { A,B,C,D } } a could know about the other nodes by calling A.parent.nodes which returns A,B,C,D W knows about all its children by calling W.nodes Lets say W has a Parent X it could know about W's children by calling X.nodes('w').nodes So in the end you can do many relational queries enabling you to interact with another comments nodes
26th Apr 2023, 1:04 PM
Anonymous
Anonymous - avatar
+ 1
Mirielle To be able to get something like (ab) replied to (comment) on post(x) you can use signals to be triggered when a Comment is created
26th Apr 2023, 1:58 PM
Anonymous
Anonymous - avatar
+ 1
Anonymous Ah, I'm not using Node.js so that's not going to help in my case. Everything is Vanilla JS so I can get more familiar with it. I'm also having trouble figuring out how from Django, I would be able to represent all those calls well and convert them to JSON. Also, no. This question is for a project I am doing on my own and Mirielle was being helpful, especially in helping me figure out the data structure.
26th Apr 2023, 2:14 PM
Justice
Justice - avatar
+ 1
Justice you can use the cdn link <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
26th Apr 2023, 2:16 PM
Anonymous
Anonymous - avatar
+ 1
Mirielle alright, though there is one thing I would like to confirm that is a onetomany relationship to self actually creates a tree structure
26th Apr 2023, 2:20 PM
Anonymous
Anonymous - avatar
+ 1
Anonymous Bob_Li I'm going to kindly ask that you guys stop answering this question now because you are giving me links to stuff I already know at this point. I have already made it clear that I will not be implementing this at the moment and I will ask that is respected. This was a great conversation and I've learned a lot but it should be done elsewhere at this point. The answers in this thread is nearly 50.
26th Apr 2023, 2:35 PM
Justice
Justice - avatar
+ 1
Bob_Li If you've done such a project before I'd love for you to share if so I could take a look. Sometimes seeing a real implementation helps me understand things better than just text. Not sure why you need to comment that I'm overthinking though when I just merely said I am not familiar with the data structure it will resemble and just want more practice.
26th Apr 2023, 2:57 PM
Justice
Justice - avatar
0
A simple way to do this is class Comment(models.Model): ... # comment fields parent = models.ForeignKey('self', on_delete=models.cascade, related_name='children')
26th Apr 2023, 10:53 AM
Anonymous
Anonymous - avatar
0
Anonymous Yeah but the API call would not be simple so I will not be doing that for now, but thank you. I'll come back to it as I said in my replies before.
26th Apr 2023, 10:56 AM
Justice
Justice - avatar