-
Notifications
You must be signed in to change notification settings - Fork 707
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
220 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
from django.db.models import Q | ||
from django.db.models import OuterRef, Subquery | ||
from django_filters import rest_framework as filters | ||
from cinema.models import MovieSession, Movie, Actor | ||
|
||
|
||
class MovieSessionFilter(filters.FilterSet): | ||
date = filters.DateFilter(field_name="show_time", lookup_expr="date") | ||
movie = filters.NumberFilter(field_name="movie__id") | ||
|
||
class Meta: | ||
model = MovieSession | ||
fields = ["date", "movie"] | ||
|
||
|
||
class MovieFilter(filters.FilterSet): | ||
genres = filters.CharFilter(method="filter_by_genre_ids") | ||
title = filters.CharFilter(field_name="title", lookup_expr="icontains") | ||
actors = filters.CharFilter(method="filter_by_actor_ids") | ||
|
||
class Meta: | ||
model = Movie | ||
fields = ["genres", "title", "actors"] | ||
|
||
def filter_by_genre_ids(self, queryset, name, value): | ||
genre_ids = [ | ||
int(id.strip()) | ||
for id in value.split(",") | ||
if id.strip().isdigit() | ||
] | ||
return queryset.filter(genres__id__in=genre_ids) | ||
|
||
def filter_by_actor_ids(self, queryset, name, value): | ||
try: | ||
# Преобразуем значения в список ID | ||
actor_ids = [ | ||
int(id.strip()) | ||
for id in value.split(",") | ||
if id.strip().isdigit() | ||
] | ||
return queryset.filter(actors__id__in=actor_ids) | ||
except ValueError: | ||
# Если передан некорректный ID | ||
return queryset.none() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# Generated by Django 4.1.13 on 2025-01-07 08:21 | ||
|
||
from django.db import migrations | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
("cinema", "0004_alter_genre_name"), | ||
] | ||
|
||
operations = [ | ||
migrations.AlterModelOptions( | ||
name="actor", | ||
options={"ordering": ["id"]}, | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# Generated by Django 5.1.4 on 2025-01-07 13:28 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
("cinema", "0005_alter_actor_options"), | ||
] | ||
|
||
operations = [ | ||
migrations.AlterField( | ||
model_name="movie", | ||
name="actors", | ||
field=models.ManyToManyField(related_name="movies", to="cinema.actor"), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.