pip install django-wishlist-blacklist (soon in pip)
Add django_wishlist_blacklist
to your INSTALLED_APPS:
INSTALLED_APPS = [
...
'django_wishlist_blacklist',
...
]
Inherit from django_wishlist_blacklist.models.WishlistAuthorModel
for your User model and
django_wishlist_blacklist.models.WishlistTargetModel
for your Product model:
from django_wishlist_blacklist.models import WishlistAuthorModel, WishlistTargetModel
class User(WishlistAuthorModel, AbstractUser)
pass
class Product(WishlistTargetModel)
pass
And we are done!
>>> product = Product.objects.first()
>>> user.is_wishlisted(product)
False
>>> user.add_to_wishlist(product)
<ModelBind: Casey Gates -> Ball(7)>
>>> user.is_wishlisted(product)
True
>>> user.get_wishlists(Product)
<QuerySet [<Ball: Ball(7)>]>
>>> user.remove_from_wishlist(product)
>>> user.get_wishlists(Product)
<QuerySet []>
Add django_wishlist_blacklist.urls
to your URL patterns:
urlpatterns = [
...
url(r'^wb/', include('django_wishlist_blacklist.urls')),
...
]
POST /wb/wishlist/add/
{
"target_ct": "product.Product",
"target_object_id": 9
}
POST /wb/wishlist/remove/
{
"target_ct": "product.Product",
"target_object_id": 9
}
from django_wishlist_blacklist.serializers import WishlistStateSerializerMixin
class ProductSerializer(WishlistStateSerializerMixin, serializers.ModelSerializer):
class Meta:
model = Product
fields = ('id', 'name', 'brand', 'created_at')
>>> serializer = ProductSerializer(product, context={'request': request})
>>> serializer.data
[
{
"id": 6,
"name": "Ball",
"brand": "Ball",
"created_at": "2020-01-01T00:00:00Z",
"is_wishlisted": True,
},
{
"id": 7,
"name": "Ball",
"brand": "Ball",
"created_at": "2020-01-01T00:00:00Z",
"is_wishlisted": False,
}
]