我使用django-follow来允许用户“关注”对象 - 在本例中为电影中的Actor。查找相关对象并显示关系
我使用
actors_user_is_following = Follow.objects.get_follows(Actor).filter(user=request.user.id)
拉回电影演员的名单,但我也想要做的是建议影片根据他们遵循演员用户。这并不需要是一个他们已经喜欢的建议相关电影的复杂算法,只是一个简单的“因为你跟着这个演员,这个演员在这部电影中,向用户建议”
我有这个现在这样做的笨拙的方式...
context['follows'] = {
'actors': Follow.objects.get_follows(Actor).filter(user=request.user.id),
'genres': Follow.objects.get_follows(Genre).filter(user=request.user.id),
}
actor_ids = []
for actor in context['follows']['actors']:
actor_ids.append(actor.target_artist_id)
genre_ids = []
for artist in context['follows']['genres']:
genre_ids.append(artist.genre_ids)
context['suggested'] = {
'films': Listing.objects.filter(Q(actors__in=actor_ids) | Q(genres__in=genre_ids))
}
哪些工作,但我敢肯定有更好的方法呢?
最重要的我也想向用户展示为什么薄膜已建议通过显示用户是继其特点是演员或流派,所以最终的结果可能是这样的......
film = {
title: 'Dodgeball'
image: '/images/films/dodgeball.jpg'
followed_actors: ['Ben Stiller', 'Vince Vaughn'] #could be multiple
followed_genres: ['Comedy'] #could be multiple
}
注意我想要返回多个电影。
这里是我的模型是如何编写了:
电影模型中定义,像这样:
from django.db import models
from app.actors.models import Actor
from app.genres.models import Genre
class Film(models.Model):
title = models.CharField(max_length=255)
strapline = models.CharField(max_length=255)
slug = models.SlugField(max_length=100)
image_url = models.CharField(max_length=255)
pub_date = models.DateTimeField('date published')
actors = models.ManyToManyField(Actor)
genres = models.ManyToManyField(Genre)
def __unicode__(self):
return self.title
和演员型号:
from django.db import models
from follow import utils
class Actor(models.Model):
title = models.CharField(max_length=255)
strapline = models.CharField(max_length=255)
image = models.CharField(max_length=255)
image_hero = models.CharField(max_length=255)
bio = models.TextField()
def __unicode__(self):
return self.title
#followable
utils.register(Actor)
您的电影模型如何定义? – karthikr
@karthikr - 更新的问题 – kieran