Skip to main content

django-fav

A simple reusable app for django that makes it easy to deal with faving and unfaving any object from any application.

It comes with a Graphene (GraphQL) Query to enable favs in your queries.

Requirements

  • Python 3.4+
  • Django 1.11

Installation

pip install django-fav
  • Add the app to your settings.py
INSTALLED_APPS = [
  ...
  "fav",
  ...
]
  • Sync your database:
python manage.py migrate

Usage:

Favorites Manager

  • Create a Favorite instance for a user and object:
>>> from django.contrib.auth.models import User
>>> from music.models import Song
>>> user = User.objects.get(username='gengue')
>>> song = Song.objects.get(pk=1)
>>> fav = Favorite.objects.create(user, song)
or:
>>> fav = Favorite.objects.create(user, 1, Song)
or:
>>> fav = Favorite.objects.create(user, 1, "music.Song")
  • Get the objects favorited by a given user:
>>> from django.contrib.auth.models import User
>>> user = User.objects.get(username='gengue')
>>> Favorite.objects.for_user(user)
>>> [<Favorite: Favorite object 1>, <Favorite: Favorite object 2>, <Favorite: Favorite object 3>]
  • Now, get user favorited objects belonging to a given model:
>>> from django.contrib.auth.models import User
>>> from music.models import Song
>>> user = User.objects.get(username='gengue')
>>> Favorite.objects.for_user(user, model=Song)
>>> [<Favorite: Favorite object 1>, <Favorite: Favorite object 2>, <Favorite: Favorite object 3>]
  • Get the favorited object instances of a given model favorited by any user:
>>> from music.models import Song
>>> Favorite.objects.for_model(Song)
>>> [<Favorite: Favorite object 1>, <Favorite: Favorite object 2>, <Favorite: Favorite object 3>]
  • Get a Favorite instance for a given object and user:
>>> from django.contrib.auth.models import User
>>> from music.models import Song
>>> user = User.objects.get(username='gengue')
>>> song = Song.objects.get(pk=1)
>>> fav = Favorite.objects.get_favorite(user, song)
  • Get all Favorite instances for a given object
>>> from music.models import Song
>>> song = Song.objects.get(pk=1)
>>> fav = Favorite.objects.for_object(song)

Graphql

In settings.py, map your grahene queries to your django models

FAV_MODELS = {
    'CurrentUser': 'core.user',
    'User': 'core.user',
    'Track': 'listen.Track',
}

Add url_renditions.graphql_schema.Query to your root query and mutation.

import graphene
import fav.graphql_schema

class Query(
        ...
        fav.graphql_schema.Query,
        graphene.ObjectType):
    pass

class Mutation(
        ...
        fav.graphql_schema.Mutation,
        graphene.ObjectType):
    pass


schema = graphene.Schema(query=Query, mutation=Mutation)

Query

Then, you can ask for:

query {
  isInUserFavorites(objectId: "VHJhY2s6OA==")
}

and you get

{
  "data": {
    "isInUserFavorites": false
  }
}

Mutation

mutation {
  favorite(input: {objectId: "VHJhY2s6OA=="}) {
    deleted
    created
  }
}

and you get

{
  "data": {
    "favorite": {
      "deleted": null,
      "created": true,
    }
  }
}

Thanks

Metadata

Release files for django-fav 0.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-fav 0.3
File Size Uploaded
django-fav-0.3.tar.gz 5.0 kB Details

Release files / django-fav-0.3.tar.gz

Download URL django-fav-0.3.tar.gz
Size 5.0 kB
Tags Source
SHA-256 checksum
How to use checksums
fc9e87e30b088663139b7de2071752e9de721686ec14ec97880e0e7a397b97e0
BLAKE2b-256 checksum
How to use checksums
ab1c168d7476a924007535a914c54cd46874bee5d9ef71e228709c3fa4a7f223
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.11.0 pkginfo/1.4.2 requests/2.18.4 setuptools/39.1.0 requests-toolbelt/0.8.0 tqdm/4.23.3 CPython/3.6.5

Release history Release notifications | RSS feed

This release

0.3 This release

1 release file

0.2

1 release file

0.1

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page