Django如何从ManyToManyField进行序列化并列出全部 [英] Django How to Serialize from ManyToManyField and List All

查看:41
本文介绍了Django如何从ManyToManyField进行序列化并列出全部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Django 1.9.1开发移动应用程序后端,我实现了关注者模型,现在我想列出用户的所有关注者,但是我目前仍坚持这样做.我也使用Django Rest Framework.

I'm developing a mobile application backend with Django 1.9.1 I implemented the follower model and now I want to list all of the followers of a user but I'm currently stuck to do that. I also use Django Rest Framework.

这是我的UserProfile模型

This is my UserProfile model

class UserProfile(models.Model):
    # Linking UserProfile to User model.
    user = models.OneToOneField(User)
    city = models.CharField(null=True, max_length=30, blank=True)
    gender = models.CharField(null=True, max_length=10, blank=True) # m for male, f for female
    # TODO: Fix the picture later.
    #  picture = models.ImageField(upload_to='profile_images', blank=True)
    age = models.IntegerField(null=True, blank=True)
    caption = models.CharField(null=True, max_length=40, blank=True)
    following_count = models.IntegerField(default=0)
    follower_count = models.IntegerField(default=0)
    post_count = models.IntegerField(default=0)
    like_count = models.IntegerField(default=0)
    date = models.DateTimeField(default=datetime.now(), blank=True)
    is_protected = models.BooleanField(default=False)
    is_facebook_created_user = models.BooleanField(default=False)
    facebook_id = models.CharField(default='', blank=True, max_length=350)
    picture_url = models.URLField(blank=True, max_length=100, null=True)
    followings = models.ManyToManyField('self', related_name='following', symmetrical=False)
    followers = models.ManyToManyField('self', related_name='follower', symmetrical=False)
    blocking = models.ManyToManyField('self', related_name='block', symmetrical=False)
    blocked_by = models.ManyToManyField('self', related_name='blocked', symmetrical=False)

    def block_user(self,username):
            other = UserProfile.objects.get(user__username=username)
            if not self.is_blocking(username):
                self.blocking.add(other)
                other.blocked_by.add(self)
                return True
            else:
                return False

    def unblock_user(self, username):
            other = UserProfile.objects.get(user__username=username)
            if self.is_blocking(username):
                self.blocking.remove(other)
                other.blocked_by.remove(self)
                return True
            else:
                return False

    def follow_user(self, username):
            other = UserProfile.objects.get(user__username=username)
            if not self.is_following(username):
                self.followings.add(other)
                self.following_count = self.followings.all().count()
                self.save()
                other.followers.add(self)
                other.follower_count = other.followers.all().count()
                other.save()
                return True
            else:
                return False

    def unfollow_user(self, username):
            other = UserProfile.objects.get(user__username=username)
            if self.is_following(username):
                self.followings.remove(other)
                self.following_count = self.followings.all().count()
                self.save()
                other.followers.remove(self)
                other.follower_count = other.followers.all().count()
                other.save()
                return True
            else:
                return False

    def is_blocking(self,username):
        return self.blockings.all().filter(user__username=username).exists()

    def is_blocked_by(self,username):
        return self.blocked_by.all().filter(user__username=username).exists()

    def is_following(self, username):  #returns Bool
        return self.followings.all().filter(user__username=username).exists()

    def is_followed_by(self, username):  #returns Bool
        return self.followers.all().filter(user__username=username).exists()

    def __unicode__(self):
        return self.user.username

    def get_token(self):
        try:
            token = Token.objects.get(user_id=self.user_id)
        except:
            token = 'error'
        return token
    def get_username(self):
        return self.user.username

如您所见,我将ManyToManyField用于关注者.现在,我想列出一个用户的关注者,但是我不想只列出他们的所有信息,因为这是不必要的.我只想列出他们的用户名,因为不需要完整的信息,这只是浪费.

As you see, I user ManyToManyField for followers. Now I want to list followers of a user but I don't want to list just all of their information since it's unnecessary. I just want to list their usernames because no need for whole information and it's just a waste.

这是我的关注者视图.

@api_view(['GET'])
def get_followers(request):
    username = request.query_params.get('username', None)
    if username is not None:
        if        UserProfile.objects.all().filter(user__username=username).exists():
            wanted = UserProfile.objects.get(user__username=username)
            followers = wanted.followers.all()
            serializer = FollowerSerializer(followers)
            return JsonResponse(serializer.data)
        else:
            return JsonResponse({"result": "user_does_not_exist"})
    else:
        #TODO: return current user's followers
        return JsonResponse({"result": "provide_username"})

我还添加了仅针对用户关注者的序列化器.

Also I added a serializer for only followers of user.

class FollowerSerializer(serializers.ModelSerializer):
    class Meta:
        model = UserProfile
        field = ('followers',)

问题是,它仅列出所有用户信息,并且不提供用户名,而仅列出其ID,而我却无能为力.

The problem is, it just lists the all of the user information and it doesn't give usernames but it only lists their id's which i can't do anything.

我忘了提到我确实考虑过使用ListAPIView,但是我不能用它来获取username参数.如果有一种获取参数的方法,我也可以将ListAPIView用于关注者.

I forgot to mention that I did think of using ListAPIView but I can't get the username parameter with that. If there is a way of getting the parameter, I could also use ListAPIView for followers.

如何解决此问题并仅列出其用户名?任何帮助将不胜感激.

How can I fix this problem and list only their usernames? Any help will be appreciated.

推荐答案

我认为您需要的是嵌套的序列化器:

I think what you need is the nested serializer:

class FollowerSerializer(serializers.ModelSerializer):
    username = serializers.CharField(source='user__username')

    class Meta:
        model = UserProfile
        fields = ('username', )


class FollowerSerializer(serializers.ModelSerializer):
    followers = FollowerSerializer(many=True, read_only=True)

    class Meta:
        model = UserProfile
        fields = ('followers', )

这篇关于Django如何从ManyToManyField进行序列化并列出全部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆