Skip to content
Advertisement

I want to get all anime related to specific Genre,

model

class Genre(models.Model):
    genre = models.CharField(max_length=100, null=True)

    def __str__(self):
        return self.genre

class anime(models.Model):
    
    anime_name = models.CharField(max_length=100, default="")
    description = models.CharField(max_length=1000, null=True)
    Genre = models.ManyToManyField(Genre)
    Season = models.IntegerField(null=True)
    Episodes = models.CharField(max_length=100,null=True)
    IMDB_Rating = models.FloatField(null=True)
    #Image =

    def __str__(self):
        return self.anime_name

view

def g(request, G):

    animes = anime.objects.filter(Genre=G)
    x = {'animes': animes}
    return render(request, 'Anime/genre.html', x)

urls

path('genre/<G>/', views.g, name="main"),`

Advertisement

Answer

You should filter on the genre field of the Genre, so:

def g(request, G):
    animes = anime.objects.filter(Genre__genre=G)
    x = {'animes': animes}
    return render(request, 'Anime/genre.html', x)

Note: Models in Django are written in PascalCase, not snake_case, so you might want to rename the model from anime to Anime.

Advertisement