From 88073c9ff207adb8b8c971a4169c38850f74dd79 Mon Sep 17 00:00:00 2001 From: Oleksandr Date: Tue, 26 Nov 2024 20:58:10 +0200 Subject: [PATCH 1/7] Solution --- db/models.py | 9 +++++++++ main.py | 29 ++++++++++++++++++++++++++++- 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/db/models.py b/db/models.py index 137941ff..dc931c25 100644 --- a/db/models.py +++ b/db/models.py @@ -1 +1,10 @@ from django.db import models + + +class Genre(models.Model): + name = models.CharField(max_length=255) + + +class Actor(models.Model): + first_name = models.CharField(max_length=255) + last_name = models.CharField(max_length=255) diff --git a/main.py b/main.py index 545f938a..cceb5683 100644 --- a/main.py +++ b/main.py @@ -2,6 +2,33 @@ from django.db.models import QuerySet +from db.models import Genre, Actor def main() -> QuerySet: - pass + + genres = ["Western", "Action", "Dramma"] + + actors = [ + ("George Klooney", "Kianu Reaves", "Scarlett Keegan"), + ("Will Smith", "Jaden Smith", "Scarlett Johansson") + ] + + for genre in genres: + Genre.objects.create(name=genre) + + for last_name, first_name in actors: + Actor.objects.create(first_name=first_name, last_name=last_name) + + Genre.objects.filter(name="Dramma").update(name="Drama") + Actor.objects.filter(first_name="George", last_name="Klooney").update( + last_name="Clooney" + ) + Actor.objects.filter( + first_name="Kianu", last_name="Reaves" + ).update( + first_name="Keanu", last_name="Reevas") + + Genre.objects.filter(name="Action").delete() + Actor.objects.filter(first_name="Scarlett").delete() + + return Actor.objects.filter(last_name="Smith").all().order_by("first_name") \ No newline at end of file From cc1337e04e3676da74bc64e0708750e9992e50e1 Mon Sep 17 00:00:00 2001 From: Oleksandr Date: Tue, 26 Nov 2024 21:27:37 +0200 Subject: [PATCH 2/7] Solution --- db/migrations/0001_initial.py | 44 +++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 db/migrations/0001_initial.py diff --git a/db/migrations/0001_initial.py b/db/migrations/0001_initial.py new file mode 100644 index 00000000..e21bc7d5 --- /dev/null +++ b/db/migrations/0001_initial.py @@ -0,0 +1,44 @@ +# Generated by Django 5.1.3 on 2024-11-26 21:19 + +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)), + ], + ), + ] From 1625a1035e143e37c7f99b4ea6e7b4e952858ea7 Mon Sep 17 00:00:00 2001 From: Oleksandr Date: Tue, 26 Nov 2024 21:30:21 +0200 Subject: [PATCH 3/7] Solution --- main.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/main.py b/main.py index cceb5683..e80f7054 100644 --- a/main.py +++ b/main.py @@ -4,6 +4,7 @@ from db.models import Genre, Actor + def main() -> QuerySet: genres = ["Western", "Action", "Dramma"] @@ -31,4 +32,4 @@ def main() -> QuerySet: Genre.objects.filter(name="Action").delete() Actor.objects.filter(first_name="Scarlett").delete() - return Actor.objects.filter(last_name="Smith").all().order_by("first_name") \ No newline at end of file + return Actor.objects.filter(last_name="Smith").all().order_by("first_name") From 092095962b5377dd10f567340b636d695d3d5506 Mon Sep 17 00:00:00 2001 From: Oleksandr Date: Tue, 26 Nov 2024 21:32:56 +0200 Subject: [PATCH 4/7] Solution --- main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.py b/main.py index e80f7054..c91effff 100644 --- a/main.py +++ b/main.py @@ -17,7 +17,7 @@ def main() -> QuerySet: for genre in genres: Genre.objects.create(name=genre) - for last_name, first_name in actors: + for first_name, last_name in actors: Actor.objects.create(first_name=first_name, last_name=last_name) Genre.objects.filter(name="Dramma").update(name="Drama") From ed4e9e26b77ec8de85e3d240371a16ad227a852d Mon Sep 17 00:00:00 2001 From: Oleksandr Date: Tue, 26 Nov 2024 21:39:40 +0200 Subject: [PATCH 5/7] Solution --- db/models.py | 2 +- main.py | 11 ++++------- 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/db/models.py b/db/models.py index dc931c25..4f0b03fa 100644 --- a/db/models.py +++ b/db/models.py @@ -7,4 +7,4 @@ class Genre(models.Model): class Actor(models.Model): first_name = models.CharField(max_length=255) - last_name = models.CharField(max_length=255) + last_name = models.CharField(max_length=255) \ No newline at end of file diff --git a/main.py b/main.py index c91effff..c4c15654 100644 --- a/main.py +++ b/main.py @@ -1,7 +1,6 @@ import init_django_orm # noqa: F401 from django.db.models import QuerySet - from db.models import Genre, Actor @@ -10,8 +9,8 @@ def main() -> QuerySet: genres = ["Western", "Action", "Dramma"] actors = [ - ("George Klooney", "Kianu Reaves", "Scarlett Keegan"), - ("Will Smith", "Jaden Smith", "Scarlett Johansson") + ("George", "Klooney"), ("Kianu", "Reaves"), ("Scarlett", "Keegan"), + ("Will", "Smith"), ("Jaden", "Smith"), ("Scarlett", "Johansson") ] for genre in genres: @@ -24,10 +23,8 @@ def main() -> QuerySet: Actor.objects.filter(first_name="George", last_name="Klooney").update( last_name="Clooney" ) - Actor.objects.filter( - first_name="Kianu", last_name="Reaves" - ).update( - first_name="Keanu", last_name="Reevas") + Actor.objects.filter(first_name="Kianu", last_name="Reaves").update( + first_name="Keanu", last_name="Reeves") Genre.objects.filter(name="Action").delete() Actor.objects.filter(first_name="Scarlett").delete() From 884b4a2fc2ace7c981c4ea28a06db39499aa96bc Mon Sep 17 00:00:00 2001 From: Oleksandr Date: Tue, 26 Nov 2024 21:43:50 +0200 Subject: [PATCH 6/7] Solution --- main.py => db/main.py | 2 +- db/{ => migrations}/models.py | 2 +- tests/test_main.py | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) rename main.py => db/main.py (95%) rename db/{ => migrations}/models.py (78%) diff --git a/main.py b/db/main.py similarity index 95% rename from main.py rename to db/main.py index c4c15654..d7e420fa 100644 --- a/main.py +++ b/db/main.py @@ -1,7 +1,7 @@ import init_django_orm # noqa: F401 from django.db.models import QuerySet -from db.models import Genre, Actor +from db.migrations.models import Genre, Actor def main() -> QuerySet: diff --git a/db/models.py b/db/migrations/models.py similarity index 78% rename from db/models.py rename to db/migrations/models.py index 4f0b03fa..dc931c25 100644 --- a/db/models.py +++ b/db/migrations/models.py @@ -7,4 +7,4 @@ class Genre(models.Model): class Actor(models.Model): first_name = models.CharField(max_length=255) - last_name = models.CharField(max_length=255) \ No newline at end of file + last_name = models.CharField(max_length=255) diff --git a/tests/test_main.py b/tests/test_main.py index 166cb487..72f3b2cf 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -1,8 +1,8 @@ import pytest -from db.models import Genre, Actor +from db.migrations.models import Genre, Actor -from main import main +from db.main import main @pytest.mark.django_db From b06daf16b0ae4d74754913abd9290a5600f06c34 Mon Sep 17 00:00:00 2001 From: Oleksandr Date: Tue, 26 Nov 2024 21:47:25 +0200 Subject: [PATCH 7/7] Solution --- db/main.py | 2 +- db/{migrations => }/models.py | 6 ++++++ db/settings.py | 5 +++++ settings.py | 20 -------------------- tests/test_main.py | 2 +- 5 files changed, 13 insertions(+), 22 deletions(-) rename db/{migrations => }/models.py (62%) create mode 100644 db/settings.py delete mode 100644 settings.py diff --git a/db/main.py b/db/main.py index d7e420fa..c4c15654 100644 --- a/db/main.py +++ b/db/main.py @@ -1,7 +1,7 @@ import init_django_orm # noqa: F401 from django.db.models import QuerySet -from db.migrations.models import Genre, Actor +from db.models import Genre, Actor def main() -> QuerySet: diff --git a/db/migrations/models.py b/db/models.py similarity index 62% rename from db/migrations/models.py rename to db/models.py index dc931c25..51af6ba9 100644 --- a/db/migrations/models.py +++ b/db/models.py @@ -4,7 +4,13 @@ class Genre(models.Model): name = models.CharField(max_length=255) + def __str__(self) -> str: + return self.name + class Actor(models.Model): first_name = models.CharField(max_length=255) last_name = models.CharField(max_length=255) + + def __str__(self) -> str: + return f"{self.first_name} {self.last_name}" \ No newline at end of file diff --git a/db/settings.py b/db/settings.py new file mode 100644 index 00000000..43201c79 --- /dev/null +++ b/db/settings.py @@ -0,0 +1,5 @@ +USE_TZ = False + +INSTALLED_APPS = ("db",) + +DEBUG = True \ No newline at end of file diff --git a/settings.py b/settings.py deleted file mode 100644 index 18760dec..00000000 --- a/settings.py +++ /dev/null @@ -1,20 +0,0 @@ -import os - -# Build paths inside the project like this: os.path.join(BASE_DIR, ...) -BASE_DIR = os.path.dirname(os.path.abspath(__file__)) - -# SECURITY WARNING: Modify this secret key if using in production! -SECRET_KEY = "6few3nci_q_o@l1dlbk81%wcxe!*6r29yu629&d97!hiqat9fa" - -DEFAULT_AUTO_FIELD = "django.db.models.AutoField" - -DATABASES = { - "default": { - "ENGINE": "django.db.backends.sqlite3", - "NAME": os.path.join(BASE_DIR, "db.sqlite3"), - } -} - -USE_TZ = False - -INSTALLED_APPS = ("db",) diff --git a/tests/test_main.py b/tests/test_main.py index 72f3b2cf..77224363 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -1,6 +1,6 @@ import pytest -from db.migrations.models import Genre, Actor +from db.models import Genre, Actor from db.main import main