0

I am working on a django project following an old guide from CoreyMSchafer (https://www.youtube.com/watch?v=UmljXZIypDc&list=PL-osiE80TeTtoQCKZ03TU5fNfx2UY6U4p), who builds a django blog application. After finishing the tutorial he created I wanted to take it a step further. In the videos he creates posts, but he does not create a way for users to comment. I want to add this, but am running into a bit of idea block. My first thought is to continue the method he employed for the posts, which would be establish a one to many relationship with the user and the post, as below.

class Post(models.Model):
    title = models.CharField(max_length=100)
    content = models.TextField(max_length=1200)
    date_posted = models.DateTimeField(default=timezone.now)
    author = models.ForeignKey(User, on_delete=models.CASCADE)

    def __str__(self):
        return self.title

    def get_absolute_url(self):
        return reverse('post-detail', kwargs={'pk': self.pk})


class Comment(models.Model):
    title = models.CharField(max_length=100)
    content = models.TextField(Max_length=1200)
    date_posted = models.DateTimeField(default=timezone.now)
    author = models.ForeignKey(User,on_delete=models.CASCADE)
    base_post = models.ForeignKey(Post, on_delete=models.CASCADE)

    def __str__(self):
        return self.title

my concern with this would be that django does not support multiple many to one relationships, any thoughts and ideas are helpful.

TLBones
  • 3
  • 3
  • did you try this ? Or did you encounter any problem ? I did the same thing and everything went good. – iliya Jun 14 '20 at 17:26
  • It seems that the model is recognized and everything seems to work- I just can't get it to display or send the comment properly. Instead, it is either sending the comment as a post or it's not displaying the form at all. – TLBones Jun 14 '20 at 17:55

0 Answers0