【问题标题】:Query Django models查询 Django 模型
【发布时间】:2011-07-19 16:05:50
【问题描述】:

我正在尝试使用 django 模型创建查询。我有 4 个模型。该功能是显示一个人的所有 Facebook 好友,这些好友还不是好友,并且没有等待好友请求。

自定义用户朋友

身份证, from_customuser_id, to_customuser_id,

Facebook 个人资料

身份证, facebook_id, custom_user_id

自定义用户

身份证, 姓名,

好友请求

身份证, 请求者(请求者的用户 ID), requestee(被请求人的user_id),

现在我有一个 facebook id 列表作为变量示例

facebook_ids = [12123,45433,455664,44445]

本质上,我试图通过 django 模型创建的查询是选择所有在 facebookprofile 表中具有 facebook id 但与用户没有成为朋友的关系或有待处理的朋友请求的自定义用户。

一个朋友被定义为在 CustomUserFriends 表中有 2 条记录,例如

朋友关系是

自定义用户

身份证

1

2

自定义用户朋友

id from_custom_user_id to_custom_user_id

1 1 2

2 2 1

【问题讨论】:

    标签: python django django-models


    【解决方案1】:

    所以,我不完全确定您在这里想要完成什么。这是在为特定用户获取所有非朋友或拥有特定用户和试图找到他所有彼此不是朋友的朋友之间折腾。我决定两者都做,让你决定你想要哪一个。

    首先,有两个功能。一个是我们将要使用的主要功能,另一个只是用于显示信息。

    def get_non_friends_for_user(u, friend_ids_filter=[]):
    
        # Search the friends lists
        friend_ids = list(CustomUserFriends.objects.filter(
            from_customuser_id=u.pk).values_list('to_customuser_id', flat=True))
        friend_ids += list(CustomUserFriends.objects.filter(
            to_customuser_id=u.pk).values_list('from_customuser_id', flat=True))
    
        # Search the requests lists
        friend_ids += list(FriendRequests.objects.filter(
            requester=u.pk).values_list('requestee', flat=True))
        friend_ids += list(FriendRequests.objects.filter(
            requestee=u.pk).values_list('requester', flat=True))
    
        non_friends = CustomUser.objects.exclude(id__in=friend_ids)
        if friend_ids_filter:
            non_friends = non_friends.filter(id__in=friend_ids_filter)
        return non_friends
    
    def display_user_info(cu, non_friends):
        print
        print cuf.name
        for non_friend in non_friends:
            print '\t', non_friend.name
    

    现在,要获取不是特定用户朋友的所有人,我们只需使用该函数

    # Get all non-friends for custom_user
    # Note that custom_user should be defined before as a CustomUsers object
    non_friends = get_non_friends_for_user(custom_user)
    display_user_info(custom_user, non_friends)
    

    要获取一个用户的好友列表,而这些好友不是该用户的另一个好友的好友,我们可以这样做:

    # Again, custom_user must already be defined as a CustomUsers object
    custom_user_non_friends = {}
    custom_user_friends = CustomUserFriends.objects.filter(
        from_customuser_id=custom_user.pk)
    friend_ids = list(custom_user_friends.values_list('to_customuser_id', flat=True))
    for cuf in custom_user_friends:
        cu = cuf.to_customuser_id
    
        # Add the queryset to the dictionary
        custom_user_non_friends[cu] = get_non_friends_for_user(cu, friend_ids)
    
    for cu, non_friends in custom_user_non_friends.items():
        display_user_info(cu, non_friends)
    

    应该这样做。我还没有测试过这些,而且这一切都在我的脑海中浮现,所以可能存在一些错误。如果它对您不起作用或不是您想要的,请发表评论,我会看看我能做些什么。

    【讨论】:

      猜你喜欢
      • 2017-08-10
      • 2015-07-13
      • 2012-12-15
      • 2018-09-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多