Quick update on django-relationships

With the 0.3.0 release of django-relationships, I've made a couple backwards-incompatible changes which I thought I'd mention.

Manager api changes

The most obvious changes are to the RelationshipManager class, which is responsible for providing the nice API when querying a user's relationships. Basically there were two sets of methods for querying -- those that queried one-way relationships (e.g. "following"), and those that queried symmetrical relationships (e.g. "friends"). I removed all the "symmetrical"-specific functions and went with the convention of having a symmetrical keyword argument for most functions.

Old API:

def get_relationships(self, status):
    return User.objects.filter(
        ...
    )

def get_related_to(self, status):
    return User.objects.filter(
        ...
    )

def get_symmetrical(self, status):
    return User.objects.filter(
        ...
    )

New API:

def get_relationships(self, status, symmetrical=False):
    """
    Returns a QuerySet of user objects with which the given user has
    established a relationship.
    """
    query = self._get_from_query(status)

    if symmetrical:
        query.update(self._get_to_query(status))

    return User.objects.filter(**query)

def get_related_to(self, status):
    """
    Returns a QuerySet of user objects which have created a relationship to
    the given user.
    """
    return User.objects.filter(**self._get_to_query(status))

In addition, I added a symmetrical keyword argument to the .add() and .remove() manager methods, so you can easily create and delete symmetrical relationships.

A summary of the changes to the manager can be seen in commit e9ad8fac.

Changes to the add and remove views

While I was combing through the code fixing references to the old API, I realized that the Add and Remove relationship views only handled one-way relationships. I fixed this by having the views check the RelationshipStatus slug:

  • /relationships/add/joe/following/ -- creates a one-way relationship to Joe
  • /relationships/add/joe/friends/ -- creates a symmetrical relationship

To create these URLs in your templates, you can use the add_relationship_url and remove_relationship_url template filters:

<!--
display a form to create a symmetrical relationship between the
currently logged-in user and "some_user"
-->

<form method="post" action="{{ some_user|add_relationship_url:"friends" }}">
  <button type="submit">Friend {{ some_user }}</button>
</form>

These changes can be seen in commit 85d12a42

Coming soon, better docs

I hope to have some more useful documentation for this project very soon, but in the meantime you can check out the somewhat sparse documentation hosted at readthedocs.

Comments (0)

Commenting has been disabled for this entry

If you'd like to discuss an aspect of this post, feel free to contact me via email.