如何使用Django模型制作跟随者跟踪系统 [英] How to make follower-following system with django model
问题描述
我是一个学习django rest框架的学生
I'm a student studying django rest framework
我正在用django rest框架做一个简单的sns
I'm making a simple sns with django rest framework
我需要关注者追踪系统。因此,我尝试做到了,但是遇到了一些麻烦
I need follower-following system. So, I tried to make it but there is some trouble
首先,这是我的用户模型,具有AbstractBaseUser和PermissionsMixin
First this is my user model with AbstractBaseUser and PermissionsMixin
class User(AbstractBaseUser, PermissionsMixin):
user_id = models.CharField(max_length=100, unique=True, primary_key=True)
name = models.CharField(max_length=100)
created_at = models.DateTimeField(auto_now_add=True)
is_staff = models.BooleanField(default=False)
followers = models.ManyToManyField('self', related_name='follower',blank=True)
following = models.ManyToManyField('self', related_name='following',blank=True)
profile_image = models.ImageField(blank=True)
外地追随者是跟随我的人,而跟随我的人是
the field followers is people who follows me and following is whom i follow
当我使用此APIView类添加以下内容
When i add following with this APIView class
class AddFollower(APIView):
permission_classes = [IsAuthenticated, ]
def post(self, requset, format=None):
user = User.objects.get(user_id=self.request.data.get('user_id'))
follow = User.objects.get(user_id=self.request.data.get('follow'))
user.following.add(follow)
user.save()
follow.followers.add(user)
follow.save()
print(str(user) + ", " + str(follow))
return JsonResponse({'status':status.HTTP_200_OK, 'data':"", 'message':"follow"+str(follow.user_id)})
user_id是我,以下是谁我想关注
The user_id is me and the follow is whom i want to follow
我想将关注添加到user_id的关注字段,并将user_id添加到关注的关注者字段
I want to add follow to user_id's following field and add user_id to follow's followers field
但这不起作用
我想要的结果是这样的(带有用户信息api)
What i want for result is like this (with user information api)
{
"followers": [],
"following": [
"some user"
],
}
某些用户的用户信息
{
"followers": [
"user above"
],
"following": [
],
}
但实际结果是这样的
{
"followers": [
"some user"
],
"following": [
"some user"
],
}
某些用户的用户信息
{
"followers": [
"user above"
],
"following": [
"user above"
],
}
这不是我想要的
我不知道这个问题,我需要一些帮助
I have no idea with this problem i need some help
谢谢
推荐答案
我会以不同的方式进行设计。
I would design it in different way.
我不会将信息添加到 User
模型中,而是显式创建另一个表来存储有关关注者的信息和以下。
I would not add the information to the User
model but explicitly create another table to store information about "followers" and "following".
表的模式为:
class UserFollowing(models.Model):
user_id = models.ForeignKey("User", related_name="following")
following_user_id = models.ForeignKey("User", related_name="followers")
# You can even add info about when user started following
created = models.DateTimeField(auto_now_add=True)
现在,在您的后方法实现中,您只需执行以下操作:
Now, in your post method implementation, you would do only this:
UserFollowing.objects.create(user_id=user.id,
following_user_id=follow.id)
那么,您可以轻松地访问关注者和关注者:
And then, you can access following and followers easily:
user = User.objects.get(id=1) # it is just example with id 1
user.following.all()
user.followers.all()
然后您可以创建约束,以便用户不能再跟随同一用户两次。但是我把这个留给你(提示:unique_together)
And you can then create constraint so user cannot follow the same user twice. But i leave this up to you ( hint: unique_together )
这篇关于如何使用Django模型制作跟随者跟踪系统的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!