0

I`m creating a simple blog now, and the main problem is to create a relation between Users. I use a default django User which should subscribe another user who is an author of post. I have only one Post model in my app

class Post(models.Model):
    author = models.ForeignKey(User, on_delete=models.CASCADE, null=True)
    title = models.CharField(max_length=128)
    content = models.TextField(blank=True)
    created_on = models.DateTimeField(auto_now_add=True)
    seen = models.ManyToManyField(User, related_name='blog_posts', blank=True)
Sasha
  • 11
  • 1
  • 3

1 Answers1

0

The relationship you're referring to isn't about the Post model as I understand it. So I think it might be better if you create a separate model. I share a model below as an idea, you can edit field names or add/delete fields according to your needs.

class AuthorSubscription(models.Model):
    author = models.OneToOneField(User, on_delete=models.CASCADE, 'author_subscription')
    subscribers = models.ManyToManyField(User, related_name='subscriptions', blank=True)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)
Ogulcan Olguner
  • 571
  • 2
  • 12
  • In sort, what the OP is trying to do is [extend the user model](https://stackoverflow.com/questions/44109/extending-the-user-model-with-custom-fields-in-django) and creating another `models.Model` class for that is one way to do it. Another more "modern" way to do it is by using another class instead of `User` that inherits from `AbstractUser` and adding the `ManyToMany` field there. – Countour-Integral Nov 29 '20 at 00:00
  • Yes, I didn't comment on that part because it was not in the question, but @Countour-Integral is absolutely right. It might be a good idea to customize the User model as [Django Doc](https://docs.djangoproject.com/en/3.1/topics/auth/customizing/#using-a-custom-user-model-when-starting-a-project) recommends. – Ogulcan Olguner Nov 29 '20 at 00:22