Answers for "How to JOIN three tables with Django ORM"

0

How to JOIN three tables with Django ORM

from django.db import models

class Artist(models.Model):
    name = models.CharField(max_length=60)
    year_established = models.SmallIntegerField()
    votes = models.IntegerField(blank=True, null=True)


class Song(models.Model):
    artist = models.ForeignKey(Artist, related_name='songs')
    title = models.CharField(max_length=5)
    votes = models.IntegerField()


class Fan(models.Model):
    artist = models.ForeignKey(Artist, related_name='fans')
    name = models.CharField(max_length=5)
    votes_casted = models.IntegerField()
Posted by: Guest on September-11-2021
0

How to JOIN three tables with Django ORM

queryset = Artist.objects.select_related(
    'songs', 'fans'
).filter(songs__title__icontains='love', fans__votes_casted__gte=100)
Posted by: Guest on September-11-2021

Code answers related to "How to JOIN three tables with Django ORM"

Python Answers by Framework

Browse Popular Code Answers by Language