English 中文(简体)
A Friendship relationship question
原标题:

I am having difficulties with listing this type of data. Scenario is as follows:

  1. user1 add s a friend called user2
  2. user2 confirms that user1 is his friend

what should happen is user2 and user1 see s each others name in their friends list. What s happening now is I am able to add user2 to user1 friends list but user1 cannot see user2 in his/her list. My question is how do I get user1 to show up in user2 s list and user2 to show up in user1 s friend list if a user has confirmed friendship? I was thinking of utilizing the confirmation status in the model and because that user1 and user2 s id is both in the confirmed relationship I don t see any integrity issues here. Any tips?

Friendship model:

class Friendship(models.Model):
        NOT_CONFIRMED = 1
        PENDING= 2
        CONFIRMED = 3

        STATUS_CHOICES = (
                (NOT_CONFIRMED,  Not Confirmed ),
                (PENDING,  Pending ),
                (CONFIRMED,  Confirmed ),
        )
        from_friend = models.ForeignKey(User, related_name= friend_set )
        to_friend = models.ForeignKey(User, related_name= to_friend_set )
        confirmed = models.IntegerField(choices=STATUS_CHOICES,
default=NOT_CONFIRMED)

        class Meta:
                unique_together = (( to_friend ,  from_friend ),)

        def __unicode__(self):
                return  %s, %s  % (self.from_friend.username,
self.to_friend.username)

Views to render the friendships (as you can see, I have been playing with the filtering):

@login_required
def friends_list(request, username):
        user = get_object_or_404(User, username=username)
        #friends = [friendship for friendship in user.friend_set.filter(Q
(confirmed=2) | Q(confirmed=3))]
        friends = Friendship.objects.filter(
                Q(to_friend=user) | Q(confirmed=3)
        )

        # get friends latest 10 shows
        friend_shows = Show.objects.filter(user__in=friends).order_by( -id )
        return render_to_response( habit/friends_list.html , {
                 user : request.user,
                 friends : friends,
                 shows : friend_shows[:10],
                 username : username,
        })
最佳回答

It s not obvious that your friendship model is associative. Is my friends list the list of all friendships in which I am the from_user? Or is it all friendships in which I am either the from_user or the to_user?

If it s the former, then every friend relationship is represented by two friendship objects, one indicating that you re my friend, and one indicating that I m your friend. Whenever a user makes a request, you ll be adding two friendships. The status will indicate whether the person making the request is the from_user or the to_user. And when a request is confirmed or rejected, you ll update both friendships to reflect this.

If it s the latter, then every friend relationship is represented by a single friendship object. In this case, your object design will need to record which friend in the relationship made the request, so that when you examine the object you can tell which user can turn the request into a confirmed friendship.

问题回答

Based upon this page: http://docs.djangoproject.com/en/dev/topics/db/queries/

The following code:

friends = Friendship.objects.filter(
        Q(to_friend=user) | Q(confirmed=3)
)

equates to: to_friend = user OR confirmed = 3. Which probably isn t what you want, based on your description.

This looks closer to what you want:

friends = Friendship.objects.filter(
        Q(to_friend=user) | Q(from_friend=user)
      , Q(confirmed=3)
)




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签