Wiki

Clone wiki

django-faves / Home

Overview

django-faves is a generic favorites app. This app lets users favorite objects in your database (as well as unfavorite them). Original author is jeffrey.a.croft. Some recent code is written by Mikhail Korobov. License is New BSD.

Installation

easy_install django-faves

or

pip install django-faves

or

hg clone http://bitbucket.org/kmike/django-faves/
cd django-faves
python setup.py install

Next, add 'faves' to INSTALLED_APPS. You may also want to plug 'faves.urls' to your urls.py patterns.

Usage

At the moment no separate documentation is provided, please read the code (and the docstrings).

TODO: write documentation. My English is not so good so contributions are welcome.

New in version 0.6

`get_favorited` template tag is added. It makes it possible to reduce DB queries to minumum - all favorites information for a list of objects is retreived using single SQL query, only necessary rows are loaded.

It produces dict

{
    <object_id1>: <Fave object1>, 
    <object_id2>: <Fave object1>,             
    ...
}

and assigns it to a context variable. Keys are id's of favorited objects, values are Fave model instances.

Syntax

    {% get_favorited [objects] by [user] of type [fave-type-slug] as [varname] %}

Example

    {% get_favorited page.object_list by request.user of type favorites as faves %}

`objects` is an iterable with django models.

To simplify work with variable returned by `get_favorited`, `is_favorited` template filter is added. `is_favorited` lookup do not procude any DB queries.

Full example

You have a list of objects to display. Each object has 'add to favorites' (or 'remove from favorites') link.

{% load faves %}
{% get_favorited page.object_list by request.user of type favorites as faves_lookup %}

{% for object in page.object_list %}
	...
	
    {% if object|is_favorited:faves_lookup %}
        <a href='{% get_unfave_url object "favorites" %}'>add to favorites</a>
    {% else %}
        <a href='{% get_fave_url object "favorites" %}'>remove from favorites</a>
    {% endif %}    
	
{% endfor %}

If you preload your objects list in view (smth. like `objects = list(objects)`) then example above will execute only 1 DB query (fetch faves list in `get_favorites`).

Example 2: `has_faved` filter

You have 'details' page for 1 object. It has 'add to favorites' (or 'remove from favorites') link.

{% load faves %}

{% if request.user|has_faved:object %}
    <a href='{% get_unfave_url object "favorites" %}'>add to favorites</a>
{% else %}
    <a href='{% get_fave_url object "favorites" %}'>remove from favorites</a>
{% endif %}    

New in version 0.6.2

`inject_faves_to` template tag is added. It is similar to `get_favorited` tag but instead of setting context variable it adds attribute with Fave instance for each favorited object in `objects` list (iterable with django models).

Syntax

{% inject_faves_to [objects] by [user] of type [fave-type-slug] as [attribute_name] %}

Example

{% load faves %}
{% inject_faves_to page.object_list by request.user of type favorites as fave %}
	
{% for object in page.object_list %}
    {{ object }}
    {% if object.fave %}
        favorited 
    {% endif %}
{% endfor %}

Updated