Note that there are some explanatory texts on larger screens.

plurals
  1. POFind related objects and display relation
    primarykey
    data
    text
    <p>I am using <a href="https://github.com/caffeinehit/django-follow" rel="noreferrer">django-follow</a> to allow users to "follow" objects - in this example, Actors in films.</p> <p>I am pulling back a list of film actors using</p> <pre><code> actors_user_is_following = Follow.objects.get_follows(Actor).filter(user=request.user.id) </code></pre> <p>But what I also want to do is suggest films to the user based on the actors they are following. This does not need to be a complex algorithm of what they already like and suggesting relative films, just a simple "because you follow this actor and this actor is in this film, suggest it to the user"</p> <p>I have this rather clunky way of doing this right now...</p> <pre><code> 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)) } </code></pre> <p>Which works, but I'm sure there is a better way of doing it? </p> <p><strong>Most importantly</strong> I also want to show the user why that film as been recommended by displaying the actors or genres it features that the user is following, so the end result might be something like...</p> <pre><code>film = { title: 'Dodgeball' image: '/images/films/dodgeball.jpg' followed_actors: ['Ben Stiller', 'Vince Vaughn'] #could be multiple followed_genres: ['Comedy'] #could be multiple } </code></pre> <p>Note I would want to return multiple films.</p> <p>Here's how my models are coded up:</p> <p>Film Model defined like so:</p> <pre><code>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 </code></pre> <p>And Actor Model:</p> <pre><code>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) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload