我有一个应用程序,用户创build页面。 我想运行一个简单的数据库查询,返回有多less用户创build了2页以上。 这实际上是我想要做的,但当然这不是正确的方法: User.objects.select_related('page__gte=2').count() 我错过了什么?
我有以下型号: class User(models.Model): name = models.Charfield() email = models.EmailField() class Friendship(models.Model): from_friend = models.ForeignKey(User) to_friend = models.ForeignKey(User) 那些模型用在下面的视图和序列化器中: class GetAllUsers(generics.ListAPIView): authentication_classes = (SessionAuthentication, TokenAuthentication) permission_classes = (permissions.IsAuthenticated,) serializer_class = GetAllUsersSerializer model = User def get_queryset(self): return User.objects.all() class GetAllUsersSerializer(serializers.ModelSerializer): is_friend_already = serializers.SerializerMethodField('get_is_friend_already') class Meta: model = User fields = ('id', 'name', 'email', 'is_friend_already',) def get_is_friend_already(self, […]