From 39e54c392890a9a78cbe23f116584c751c6a6ea5 Mon Sep 17 00:00:00 2001 From: Ruslan Toister Date: Thu, 19 Dec 2024 23:23:11 +0200 Subject: [PATCH 1/2] =?UTF-8?q?=D0=9E=D0=BF=D0=B8=D1=81=20=D0=B7=D0=BC?= =?UTF-8?q?=D1=96=D0=BD,=20=D0=BD=D0=B0=D0=BF=D1=80=D0=B8=D0=BA=D0=BB?= =?UTF-8?q?=D0=B0=D0=B4:=20=D0=94=D0=BE=D0=B4=D0=B0=D0=BD=D0=BE=20=D1=81?= =?UTF-8?q?=D1=82=D0=B2=D0=BE=D1=80=D0=B5=D0=BD=D0=BD=D1=8F=20=D0=B6=D0=B0?= =?UTF-8?q?=D0=BD=D1=80=D1=96=D0=B2=20=D1=96=20=D0=B0=D0=BA=D1=82=D0=BE?= =?UTF-8?q?=D1=80=D1=96=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- db/migrations/0001_initial.py | 29 ++++++++++++ db/migrations/0002_alter_genre_name.py | 18 ++++++++ db/models.py | 15 +++++++ main.py | 62 +++++++++++++++++++++++++- 4 files changed, 122 insertions(+), 2 deletions(-) create mode 100644 db/migrations/0001_initial.py create mode 100644 db/migrations/0002_alter_genre_name.py diff --git a/db/migrations/0001_initial.py b/db/migrations/0001_initial.py new file mode 100644 index 00000000..efddfc4a --- /dev/null +++ b/db/migrations/0001_initial.py @@ -0,0 +1,29 @@ +# Generated by Django 4.0.2 on 2024-12-19 21:06 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + ] + + operations = [ + migrations.CreateModel( + name='Actor', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('first_name', models.CharField(max_length=255)), + ('last_name', models.CharField(max_length=255)), + ], + ), + migrations.CreateModel( + name='Genre', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('name', models.CharField(max_length=255)), + ], + ), + ] diff --git a/db/migrations/0002_alter_genre_name.py b/db/migrations/0002_alter_genre_name.py new file mode 100644 index 00000000..dab13fa9 --- /dev/null +++ b/db/migrations/0002_alter_genre_name.py @@ -0,0 +1,18 @@ +# Generated by Django 4.0.2 on 2024-12-19 23:16 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('db', '0001_initial'), + ] + + operations = [ + migrations.AlterField( + model_name='genre', + name='name', + field=models.CharField(max_length=255, unique=True), + ), + ] diff --git a/db/models.py b/db/models.py index 137941ff..849bd066 100644 --- a/db/models.py +++ b/db/models.py @@ -1 +1,16 @@ from django.db import models + + +class Genre(models.Model): + name = models.CharField(max_length=255, unique=True) + + def __str__(self): + return self.name + + +class Actor(models.Model): + first_name = models.CharField(max_length=255) + last_name = models.CharField(max_length=255) + + def __str__(self): + return f"{self.first_name} {self.last_name}" diff --git a/main.py b/main.py index 545f938a..c03b0117 100644 --- a/main.py +++ b/main.py @@ -1,7 +1,65 @@ import init_django_orm # noqa: F401 from django.db.models import QuerySet - +from db.models import Genre, Actor def main() -> QuerySet: - pass + # Create genres + genres = [("Western",), ("Action",), ("Dramma",)] + for (genre_name,) in genres: + Genre.objects.get_or_create(name=genre_name) + + # Create actors + actors = [ + ("George", "Klooney"), + ("Kianu", "Reaves"), + ("Scarlett", "Keegan"), + ("Will", "Smith"), + ("Jaden", "Smith"), + ("Scarlett", "Johansson"), + ] + for first_name, last_name in actors: + Actor.objects.get_or_create(first_name=first_name, last_name=last_name) + + # Update genre name "Dramma" to "Drama" + try: + genre = Genre.objects.get(name="Dramma") + genre.name = "Drama" + genre.save() + except Genre.DoesNotExist: + print("Genre 'Dramma' not found.") + + try: + actor = Actor.objects.get(first_name="George", last_name="Klooney") + actor.last_name = "Clooney" + actor.save() + except Actor.DoesNotExist: + print("Actor 'George Klooney' not found.") + + # Update actor Kianu Reaves to Keanu Reeves + try: + actor = Actor.objects.get(first_name="Kianu", last_name="Reaves") + actor.first_name = "Keanu" + actor.last_name = "Reeves" + actor.save() + except Actor.DoesNotExist: + print("Actor 'Kianu Reaves' not found.") + + # Delete the genre "Action" + try: + Genre.objects.filter(name="Action").delete() + except Genre.DoesNotExist: + print("Genre 'Action' not found.") + + # Delete all actresses with first_name "Scarlett" + Actor.objects.filter(first_name="Scarlett").delete() + + # QuerySet of actors with last_name "Smith", ordered by first_name + smith_actors = Actor.objects.filter(last_name="Smith").order_by("first_name") + + # Return or print the QuerySet + print("Actors with last name 'Smith':") + for actor in smith_actors: + print(f'{actor.first_name} {actor.last_name}') + + return smith_actors From 1a21cd0f0a3368f2ebf46a61d1dfc0913a2b2466 Mon Sep 17 00:00:00 2001 From: Ruslan Toister Date: Thu, 19 Dec 2024 23:32:34 +0200 Subject: [PATCH 2/2] Solution2 --- db/models.py | 4 ++-- main.py | 18 +++++++++++++----- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/db/models.py b/db/models.py index 849bd066..238106ac 100644 --- a/db/models.py +++ b/db/models.py @@ -4,7 +4,7 @@ class Genre(models.Model): name = models.CharField(max_length=255, unique=True) - def __str__(self): + def __str__(self) -> str: return self.name @@ -12,5 +12,5 @@ class Actor(models.Model): first_name = models.CharField(max_length=255) last_name = models.CharField(max_length=255) - def __str__(self): + def __str__(self) -> str: return f"{self.first_name} {self.last_name}" diff --git a/main.py b/main.py index c03b0117..6a950550 100644 --- a/main.py +++ b/main.py @@ -3,6 +3,7 @@ from django.db.models import QuerySet from db.models import Genre, Actor + def main() -> QuerySet: # Create genres genres = [("Western",), ("Action",), ("Dramma",)] @@ -30,7 +31,9 @@ def main() -> QuerySet: print("Genre 'Dramma' not found.") try: - actor = Actor.objects.get(first_name="George", last_name="Klooney") + actor = Actor.objects.get( + first_name="George", last_name="Klooney" + ) actor.last_name = "Clooney" actor.save() except Actor.DoesNotExist: @@ -38,7 +41,9 @@ def main() -> QuerySet: # Update actor Kianu Reaves to Keanu Reeves try: - actor = Actor.objects.get(first_name="Kianu", last_name="Reaves") + actor = Actor.objects.get( + first_name="Kianu", last_name="Reaves" + ) actor.first_name = "Keanu" actor.last_name = "Reeves" actor.save() @@ -54,12 +59,15 @@ def main() -> QuerySet: # Delete all actresses with first_name "Scarlett" Actor.objects.filter(first_name="Scarlett").delete() - # QuerySet of actors with last_name "Smith", ordered by first_name - smith_actors = Actor.objects.filter(last_name="Smith").order_by("first_name") + smith_actors = Actor.objects.filter( + last_name="Smith" + ).order_by( + "first_name" + ) # Return or print the QuerySet print("Actors with last name 'Smith':") for actor in smith_actors: - print(f'{actor.first_name} {actor.last_name}') + print(f"{actor.first_name} {actor.last_name}") return smith_actors