-
Notifications
You must be signed in to change notification settings - Fork 1.5k
'Solution' #1721
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
base: master
Are you sure you want to change the base?
'Solution' #1721
Changes from all commits
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,100 @@ | ||
| # Generated by Django 6.0.5 on 2026-05-11 12:38 | ||
|
|
||
| import django.db.models.deletion | ||
| from django.db import migrations, models | ||
|
|
||
|
|
||
| class Migration(migrations.Migration): | ||
|
|
||
| initial = True | ||
|
|
||
| dependencies = [] | ||
|
|
||
| operations = [ | ||
| migrations.CreateModel( | ||
| name="Guild", | ||
| fields=[ | ||
| ( | ||
| "id", | ||
| models.AutoField( | ||
| auto_created=True, | ||
| primary_key=True, | ||
| serialize=False, | ||
| verbose_name="ID", | ||
| ), | ||
| ), | ||
| ("name", models.CharField(max_length=255, unique=True)), | ||
| ("description", models.TextField(null=True)), | ||
| ], | ||
| ), | ||
| migrations.CreateModel( | ||
| name="Race", | ||
| fields=[ | ||
| ( | ||
| "id", | ||
| models.AutoField( | ||
| auto_created=True, | ||
| primary_key=True, | ||
| serialize=False, | ||
| verbose_name="ID", | ||
| ), | ||
| ), | ||
| ("name", models.CharField(max_length=255, unique=True)), | ||
| ("description", models.TextField(blank=True)), | ||
| ], | ||
| ), | ||
| migrations.CreateModel( | ||
| name="Player", | ||
| fields=[ | ||
| ( | ||
| "id", | ||
| models.AutoField( | ||
| auto_created=True, | ||
| primary_key=True, | ||
| serialize=False, | ||
| verbose_name="ID", | ||
| ), | ||
| ), | ||
| ("nickname", models.CharField(max_length=255, unique=True)), | ||
| ("email", models.EmailField(max_length=255)), | ||
| ("bio", models.CharField(max_length=255)), | ||
| ("created_at", models.DateTimeField(auto_now_add=True)), | ||
| ( | ||
| "guild", | ||
| models.ForeignKey( | ||
| null=True, | ||
| on_delete=django.db.models.deletion.SET_NULL, | ||
| to="db.guild", | ||
| ), | ||
| ), | ||
| ( | ||
| "race", | ||
| models.ForeignKey( | ||
| on_delete=django.db.models.deletion.CASCADE, to="db.race" | ||
| ), | ||
| ), | ||
| ], | ||
| ), | ||
| migrations.CreateModel( | ||
| name="Skill", | ||
| fields=[ | ||
| ( | ||
| "id", | ||
| models.AutoField( | ||
| auto_created=True, | ||
| primary_key=True, | ||
| serialize=False, | ||
| verbose_name="ID", | ||
| ), | ||
| ), | ||
| ("name", models.CharField(max_length=255, unique=True)), | ||
| ("bonus", models.CharField(max_length=255)), | ||
| ( | ||
| "race", | ||
| models.ForeignKey( | ||
| on_delete=django.db.models.deletion.CASCADE, to="db.race" | ||
| ), | ||
| ), | ||
| ], | ||
| ), | ||
| ] | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| # Generated by Django 6.0.5 on 2026-05-11 14:44 | ||
|
|
||
| import django.db.models.deletion | ||
| from django.db import migrations, models | ||
|
|
||
|
|
||
| class Migration(migrations.Migration): | ||
|
|
||
| dependencies = [ | ||
| ("db", "0001_initial"), | ||
| ] | ||
|
|
||
| operations = [ | ||
| migrations.AlterField( | ||
| model_name="player", | ||
| name="guild", | ||
| field=models.ForeignKey( | ||
| null=True, | ||
| on_delete=django.db.models.deletion.SET_NULL, | ||
| related_name="members", | ||
| to="db.guild", | ||
| ), | ||
| ), | ||
| migrations.AlterField( | ||
| model_name="player", | ||
| name="race", | ||
| field=models.ForeignKey( | ||
| on_delete=django.db.models.deletion.CASCADE, | ||
| related_name="players", | ||
| to="db.race", | ||
| ), | ||
| ), | ||
| migrations.AlterField( | ||
| model_name="skill", | ||
| name="race", | ||
| field=models.ForeignKey( | ||
| on_delete=django.db.models.deletion.CASCADE, | ||
| related_name="skills", | ||
| to="db.race", | ||
| ), | ||
| ), | ||
| ] |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,39 @@ | ||
| from django.db import models | ||
|
|
||
|
|
||
| class Race(models.Model): | ||
| name = models.CharField(max_length=255, unique=True) | ||
| description = models.TextField(blank=True) | ||
|
|
||
|
|
||
| class Skill(models.Model): | ||
| name = models.CharField(max_length=255, unique=True) | ||
| bonus = models.CharField(max_length=255) | ||
| race = models.ForeignKey( | ||
| Race, | ||
| on_delete=models.CASCADE, | ||
| related_name="skills" | ||
| ) | ||
|
|
||
|
|
||
| class Guild(models.Model): | ||
| name = models.CharField(max_length=255, unique=True) | ||
| description = models.TextField(null=True) | ||
|
|
||
|
|
||
| class Player(models.Model): | ||
| nickname = models.CharField(max_length=255, unique=True) | ||
| email = models.EmailField(max_length=255) | ||
| bio = models.CharField(max_length=255) | ||
| race = models.ForeignKey( | ||
| Race, | ||
| on_delete=models.CASCADE, | ||
| related_name="players" | ||
| ) | ||
| guild = models.ForeignKey( | ||
| Guild, | ||
| on_delete=models.SET_NULL, | ||
| null=True, | ||
| related_name="members" | ||
| ) | ||
| created_at = models.DateTimeField(auto_now_add=True) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,39 @@ | ||
| import json | ||
| import init_django_orm # noqa: F401 | ||
|
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. This import is never used. The models use |
||
|
|
||
| from db.models import Race, Skill, Player, Guild | ||
|
|
||
|
|
||
| def main() -> None: | ||
| pass | ||
| with open("players.json", "r") as file: | ||
| players_info = json.load(file) | ||
|
|
||
| for nickname, user_info in players_info.items(): | ||
| race_obj, _ = Race.objects.get_or_create( | ||
| name=user_info["race"]["name"], | ||
| defaults={"description": user_info["race"]["description"]} | ||
|
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. Missing |
||
| ) | ||
| for skill_data in user_info["race"].get("skills", []): | ||
| Skill.objects.get_or_create( | ||
| name=skill_data["name"], | ||
| defaults={ | ||
| "bonus": skill_data["bonus"], | ||
| "race": race_obj | ||
| } | ||
| ) | ||
| guild_obj = None | ||
| if user_info.get("guild"): | ||
| guild_obj, _ = Guild.objects.get_or_create( | ||
|
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. Missing |
||
| name=user_info["guild"]["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. Missing |
||
| defaults={"description": user_info["guild"]["description"]} | ||
| ) | ||
| Player.objects.create( | ||
| nickname=nickname, | ||
| email=user_info["email"], | ||
| bio=user_info["bio"], | ||
| race=race_obj, | ||
| guild=guild_obj | ||
| ) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
|
|
||
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.
Checklist item #5 requires using
.get()method to check whether key is defined in dictionary. Line 14 uses direct bracket accessuser_info["race"]["description"]. Consider using.get()here as well, e.g.,user_info["race"].get("description"), to match the pattern used on lines 16 and 25.