-
Notifications
You must be signed in to change notification settings - Fork 1.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
Test(py-genres-and-actors) #529
base: master
Are you sure you want to change the base?
Changes from 2 commits
6ade0fe
226a8e3
220d3c6
8bfa3b8
d9d8f3e
974b811
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# Generated by Django 4.0.2 on 2023-10-07 13:48 | ||
|
||
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)), | ||
], | ||
), | ||
] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,53 @@ | ||
import init_django_orm # noqa: F401 | ||
|
||
from db.models import Genre, Actor | ||
from django.db.models import QuerySet | ||
|
||
|
||
def main() -> QuerySet: | ||
pass | ||
create_genres = ["Western", "Action", "Dramma"] | ||
for genre in create_genres: | ||
Genre.objects.create(name=genre) | ||
|
||
create_actors = [ | ||
{"first_name": "George", "last_name": "Klooney"}, | ||
{"first_name": "Kianu", "last_name": "Reaves"}, | ||
{"first_name": "Scarlett", "last_name": "Keegan"}, | ||
{"first_name": "Will", "last_name": "Smith"}, | ||
{"first_name": "Jaden", "last_name": "Smith"}, | ||
{"first_name": "Scarlett", "last_name": "Johansson"} | ||
] | ||
for actor_data in create_actors: | ||
Actor.objects.create(**actor_data) | ||
|
||
update_genres = {"Dramma": "Drama"} | ||
for old_name, new_name in update_genres.items(): | ||
Genre.objects.filter(name=old_name).update(name=new_name) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is no need to create an |
||
|
||
update_actors = [ | ||
{"last_name": "Klooney", "new_last_name": "Clooney"}, | ||
{"first_name": "Kianu", "new_first_name": "Keanu", | ||
"last_name": "Reaves", "new_last_name": "Reeves"} | ||
] | ||
for actor_data in update_actors: | ||
filter_fields = { | ||
key: value for key, value in actor_data.items() if | ||
not key.startswith("new_") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why do you make this check? |
||
} | ||
update_fields = { | ||
key.replace("new_", ""): value for key, value in | ||
actor_data.items() if key.startswith("new_") | ||
} | ||
|
||
Actor.objects.filter(**filter_fields).update(**update_fields) | ||
|
||
delete_genres = ["Action"] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why do you need create variable. You can make it directly |
||
for genre_name in delete_genres: | ||
Genre.objects.filter(name=genre_name).delete() | ||
|
||
delete_actors = {"first_name": "Scarlett"} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is no need to create a |
||
Actor.objects.filter(**delete_actors).delete() | ||
|
||
last_name_is_smith = Actor.objects.filter( | ||
last_name="Smith").order_by("first_name") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Improve your line breaking so that |
||
|
||
return last_name_is_smith |
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.
you can create tuple and use unpacking in for loop