Skip to content
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions db/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# Generated by Django 6.0.5 on 2026-05-07 15:43

import django.db.models.deletion
from django.db import migrations, models


class Migration(migrations.Migration):

initial = True
Comment on lines +8 to +9
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Context manager contains too much logic. Move players_info = json.load(f) and for loop OUTSIDE the with block. Only file reading should be inside the context manager (checklist item #6)


dependencies = [
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This violates checklist item #3: 'Do not forget to add related_name to ForeignKey fields'. The Skill model should have a related_name like related_name='skills' to access skills from a Race instance.

]

operations = [
migrations.CreateModel(
name='Guild',
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Migration is missing related_name='players' parameter on the Player.guild ForeignKey field. The model has this parameter, but the migration doesn't reflect it.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing related_name='players' on guild ForeignKey in this migration too

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(
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This violates checklist item #3: 'Do not forget to add related_name to ForeignKey fields'. The Player model should have a related_name like related_name='players' on this field.

name='Race',
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This violates checklist item #3: 'Do not forget to add related_name to ForeignKey fields'. The Player model should have a related_name like related_name='players' on this field.

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)),
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing related_name='players' on guild ForeignKey - this prevents reverse relationship access like guild.players.all()

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This migration uses PROTECT but the model uses SET_NULL with null=True. Regenerate migrations to sync them with models.py

('guild', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, to='db.guild')),
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Migration is missing related_name='players' parameter on the Player.race ForeignKey field. The model has this parameter, but the migration doesn't reflect it.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing related_name='players' on race ForeignKey

('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)),
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing related_name='skills' on race ForeignKey

('race', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='db.race')),
],
),
]
19 changes: 19 additions & 0 deletions db/migrations/0002_alter_player_guild.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Generated by Django 6.0.5 on 2026-05-07 15:46

import django.db.models.deletion
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('db', '0001_initial'),
]
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This violates checklist item #3: 'Do not forget to add related_name to ForeignKey fields'. Add related_name to the race ForeignKey to allow reverse access from Race to Skill.


operations = [
migrations.AlterField(
model_name='player',
name='guild',
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using PROTECT instead of SET_NULL for guild on_delete. The second migration changes this to SET_NULL, but it should be SET_NULL from the start. Also missing related_name='players'. Should be: models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='players', to='db.guild')

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing related_name='players' on Player.guild ForeignKey in the AlterField operation. Add related_name='players' to match the model definition.

field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, to='db.guild'),
Comment on lines +16 to +17
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Migration 0002_alter_player_guild.py doesn't include related_name for Player.guild ForeignKey. This migration needs to be regenerated to include related_name="players".

),
]
25 changes: 25 additions & 0 deletions db/models.py
Original file line number Diff line number Diff line change
@@ -1 +1,26 @@
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)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This violates checklist item #3: 'Do not forget to add related_name to ForeignKey fields'. The race ForeignKey is missing related_name.

race = models.ForeignKey(Race, on_delete=models.CASCADE, related_name="skills")

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing related_name='skills' on Player.guild ForeignKey - migrations don't match the model definition at lines 31-35 in models.py. The migration at line 16 only has to='db.guild' without the related_name parameter.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing related_name='players' on Player.guild ForeignKey - migrations don't match the model definition at lines 31-35 in models.py.


class Guild(models.Model):
name = models.CharField(max_length=255, unique=True)
description = models.TextField(null=True)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Migration is missing related_name="players" on the Player.guild ForeignKey. The model has this, but the migration doesn't reflect it.



class Player(models.Model):
nickname = models.CharField(max_length=255, unique=True)
email = models.EmailField(max_length=255)
bio = models.CharField(max_length=255)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This violates checklist item #3: 'Do not forget to add related_name to ForeignKey fields'. The race ForeignKey is missing related_name.

race = models.ForeignKey(Race, on_delete=models.CASCADE, related_name="players")
guild = models.ForeignKey(Guild, on_delete=models.SET_NULL, null=True, related_name="players")
created_at = models.DateTimeField(auto_now_add=True)
35 changes: 34 additions & 1 deletion main.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,43 @@
import json
import init_django_orm # noqa: F401

from db.models import Race, Skill, Player, Guild


def main() -> None:
pass
with open("players.json", "r") as f:
players_info = json.load(f)
for nickname, player_info in players_info.items():
race_name = player_info["race"]["name"]
race_desc = player_info["race"].get("description", "")
Comment on lines +11 to +12
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing related_name on the race ForeignKey. According to checklist item #3, you must add related_name to all ForeignKey fields to allow access to related models on the Many side.

race, _ = Race.objects.get_or_create(
name=race_name,
defaults={"description": race_desc}
)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing related_name parameter. The model defines related_name='players' but this migration doesn't include it.


Comment on lines +16 to +17
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Migration 0002_alter_player_guild.py is missing related_name parameter for Player.guild ForeignKey. The model has related_name='players' but the migration doesn't include it. Regenerate this migration to include related_name='players'.

guild_info = player_info.get("guild")
guild = None
if guild_info:
guild, _ = Guild.objects.get_or_create(
name=guild_info["name"],
defaults={"description": guild_info.get("description")})

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing related_name on the race ForeignKey. According to checklist item #3, you must add related_name to all ForeignKey fields to allow access to related models on the Many side.

skills = []
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing related_name on the guild ForeignKey. According to checklist item #3, you must add related_name to all ForeignKey fields to allow access to related models on the Many side.

for skill_info in player_info["race"].get("skills", []):
skill, _ = Skill.objects.get_or_create(
name=skill_info["name"],
race=race,
defaults={"bonus": skill_info["bonus"]}
)
skills.append(skill)
Comment on lines +24 to +32
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The skills list is populated but never used. Consider removing this unused variable since it's not required by the task requirements.


Player.objects.create(
nickname=nickname,
email=player_info["email"],
bio=player_info["bio"],
race=race,
guild=guild,
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing related_name parameter. The model defines related_name='players' but this migration doesn't include it.

)
Comment on lines +39 to +40
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Migration 0001_initial.py is missing related_name parameter for Player.race ForeignKey. The model has related_name='players' but the migration doesn't include it.



if __name__ == "__main__":
Expand Down
Loading