简体   繁体   中英

Query Django models

I am trying to create a query using django models. I have 4 models. The functionality is to display all a person's facebook friends that are not already friends and do not have a friend request waiting.

CustomUserFriends

id, from_customuser_id, to_customuser_id,

FacebookProfile

id, facebook_id, custom_user_id

CustomUser

id, name,

FriendRequests

id, requester (user_id of the person requesting), requestee (user_id of the person requested),

Now I have a list of facebook ids as a variable example

facebook_ids = [12123,45433,455664,44445]

Essentially the query im trying to create through django models is select all customusers that have a facebook id in the facebookprofile table but do not have the relationship of being a friend with the user already or have a pending friend request.

A friend is defined as having 2 records in the CustomUserFriends table, example

a friend relationship is

CustomUsers

id

1

2

CustomUserFriends

id from_custom_user_id to_custom_user_id

1 1 2

2 2 1

So, I wasn't entirely sure what you were trying to accomplish here. It was a toss up between getting all non-friends for a particular user or having a particular user and trying to find all of his friends who weren't friends with each other. I decided I'd do both and let you decide which one you wanted.

First, there are two functions. One is the main function we'll be using, the other is just for displaying the info.

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

Now, to get all people who are not friends of a particular user we just use that function

# 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)

To get the list of a user's friends that aren't friends with another of the user's friends, we can do this:

# 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)

And that should do it. I haven't tested any of this and it's all pretty much coming off the top of my head, so there may be some bugs. If it doesn't work for you or it's not what you were looking for, just post a comment and I'll see what I can do.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM