实现关注者和关注者无法正常工作

时间:2019-11-29 19:43:27

标签: python django django-rest-framework

我有一个用户模型,其中有一个用于查询的字段。我正在尝试在其余API中将用户关注者和关注者作为其自己的模型来实现,因此我可以保存有关关注者和关注者的更多信息,并且由于我试图构建一个大型项目,因此我认为这是一个更好的做法。

问题在于该代码不起作用,并且用户尝试遵循该用户时也无法遵循该用户。

如果用户希望我关注他,并且该用户无法关注他自己并且只关注一次,那么我会希望

models.py

class UserFollowing(models.Model):
    following = models.ForeignKey(settings.AUTH_USER_MODEL, related_name="follows", null=False, blank=False, on_delete=models.DO_NOTHING)
    followers = models.ForeignKey(settings.AUTH_USER_MODEL, related_name="followed_by", null=False, blank=False, on_delete=models.DO_NOTHING)
    followed_on = models.DateTimeField(auto_now_add=True)

serializers.py

class UserFollowingSerializer(serializers.ModelSerializer):
    following_count = serializers.SerializerMethodField(read_only=True)
    followers_count = serializers.SerializerMethodField(read_only=True)
    user_is_following = serializers.SerializerMethodField(read_only=True)
    followed_on = serializers.SerializerMethodField(read_only=True)

    class Meta:
        model = UserFollowing
        fields = "__all__"

    def get_followed_on(self, instance):
        return instance.followed_on.strftime("%d %B, %Y")

    def get_following_count(self, instance):
        return instance.following.count()

    def get_followers_count(self, instance):
        return instance.followers.count()

    def get_user_is_following(self, instance):
        request = self.context.get("request")
        return instance.following.filter(slug=request.user.slug).exists()

views.py

class UserFollowAPIView(APIView):
    serializer_class = UserFollowingSerializer
    permission_classes = [IsAuthenticated]

    def delete(self, request, pk):
        user = get_user_model().objects.get("slug")
        UserFollowing.objects.delete(user_id=user, following_user_id=user)

    def post(self, request, pk):
        user = get_user_model().objects.get("slug")
        UserFollowing.objects.create(user_id=user, following_user_id=user)

urls.py

path("<slug:slug>/follow/", UserFollowAPIView.as_view(), name="users-follow"),

非常感谢您

1 个答案:

答案 0 :(得分:1)

models.py

class UserFollowing(models.Model):                            
    user = models.ForeignKey(settings.AUTH_USER_MODEL)
    following = models.ManyToMany(settings.AUTH_USER_MODEL, related_name="follows",null=False, blank=False, on_delete=models.DO_NOTHING)
    followers = models.ManyToMany(settings.AUTH_USER_MODEL, related_name="followed_by",null=False, blank=False, on_delete=models.DO_NOTHING)
    followed_on = models.DateTimeField(auto_now_add=True)

serializers.py

class UserFollowingSerializer(serializers.ModelSerializer):
    following_count = serializers.SerializerMethodField(read_only=True)
    followers_count = serializers.SerializerMethodField(read_only=True)
    user_is_following = serializers.SerializerMethodField(read_only=True)
    followed_on = serializers.SerializerMethodField(read_only=True)

class Meta:
    model = UserFollowing
    fields = "__all__"

def get_followed_on(self, instance):
    return instance.followed_on.strftime("%d %B, %Y")

def get_following_count(self, instance):
    return instance.following.count()

def get_followers_count(self, instance):
    return instance.followers.count()

def get_user_is_following(self, instance):
    request = self.context.get("request")
    return instance.following.filter(slug=request.user.slug).exists()

views.py

class UserFollowAPIView(APIView):
    serializer_class = UserFollowingSerializer
    permission_classes = [IsAuthenticated]

def delete(self, request, pk):
    user = get_user_model().objects.get("slug")
    user.objects.delete(user_id=user, following_user_id=user)

def post(self, request, pk):
    user = UserFollowing.objects.get(user=request.user)  #this is the user that wants to follow someone
    who_to_follow = UserFollowing.objects.get('slug')  #i hope the slug is the user to be followed thoo ..user being followed
    user.followers.add(who_to_follow)

urls.py

path("<slug:slug>/follow/", UserFollowAPIView.as_view(), name="users-follow"),

我还没有测试这段代码.....所以它只是直觉....但它应该可以工作