vianneyba
vianneyba

Reputation: 13

INNER JOIN with DJANGO framework

Two weeks I try to make a JOIN with django

class Groupe(models.Model):
	nomgroupe = models.CharField(max_length=50)

	def __str__(self):
		return self.nomgroupe

class Album(models.Model):
	code = models.CharField(max_length=50)
	nomgroupe = models.ForeignKey(Groupe, on_delete=models.CASCADE )
	nomalbum = models.CharField(max_length=100)

	def __str__(self):
		return self.nomalbum

I would like to search the albums of several groups. For example starting with "iron" In SQL:

SELECT * FROM MP3_album INNER JOIN MP3_groupe ON MP3_album.nomgroupe_id = MP3_groupe.id WHERE MP3_groupe.nomgroupe LIKE '%iron%';

But with DJANGO I only get there with a hard code to read

can you help me?

ps: Excuse me my english please

Upvotes: 1

Views: 38

Answers (1)

Tiny.D
Tiny.D

Reputation: 6556

You can use filter and contains/icontains:

Album.objects.filter(nomgroupe__nomgroupe__contains='iron')

Upvotes: 1

Related Questions