-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Solution #1122
Open
RToister
wants to merge
2
commits into
mate-academy:master
Choose a base branch
from
RToister:develop
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Solution #1122
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,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)), | ||
], | ||
), | ||
] |
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 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), | ||
), | ||
] |
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 |
---|---|---|
@@ -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}" |
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 |
---|---|---|
@@ -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 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
Genre.DoesNotExist
exception will not be raised by thefilter().delete()
method. This method does not raise exceptions if no records are found; it simply does nothing. Therefore, the try-except block is unnecessary here.