我需要外键查询集的帮助

时间:2019-01-28 06:51:16

标签: django django-queryset mode

我有这个模特

class UserInfo(models.Model) :
    userNumber = models.CharField(max_length=15)
    userPin = models.CharField(max_length=7, unique=True)


    def __str__(self) :
        return str(self.userPin)


class UserFollows(models.Model) :
    following = models.ForeignKey(UserInfo, on_delete=models.CASCADE, related_name='UserFollows.following+')
    followers = models.ForeignKey(UserInfo, on_delete=models.CASCADE, related_name='UserFollows.followers+')

我需要userPin的内部联接。 此命令尝试错误

UserInfo.objects.filter(userPin__UserFollows__followers= '****')

Unsupported lookup 'UserFollows' for CharField or join on the field not permitted

1 个答案:

答案 0 :(得分:0)

在模型中,userPin是CharField,而不是FK。您可以通过反向关系加入UserInfoUserFollows并用userPin进行过滤:

UserInfo.objects.filter(user_follows__followers__userPin='value')