Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
39 changes: 39 additions & 0 deletions db/models.py
Original file line number Diff line number Diff line change
@@ -1 +1,40 @@
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, blank=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.

Use .get() method instead of bracket notation for dictionary access per checklist item #5.


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,
blank=True,
related_name="members"
)
created_at = models.DateTimeField(auto_now_add=True)
37 changes: 35 additions & 2 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,43 @@
import init_django_orm # noqa: F401

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


def main() -> None:
pass
with open("players.json", "r") as f:
players = json.load(f)

for nickname, data in players.items():
race_data = data.get("race", {})
race, _ = Race.objects.get_or_create(
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 - violates CHECKLIST ITEM #3. Add related_name='skills' to enable access from Race to related Skill objects.

name=race_data.get("name"),
defaults={"description": race_data.get("description", "")}
)

for skill_data in race_data.get("skills", []):
Skill.objects.get_or_create(
name=skill_data.get("name"),
race=race,
defaults={"bonus": skill_data.get("bonus")}
)

guild = None
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 - violates CHECKLIST ITEM #3. Add related_name='players' to enable access from Race to related Player objects.

guild_data = data.get("guild")
if guild_data:
guild, _ = Guild.objects.get_or_create(
name=guild_data.get("name"),
defaults={"description": guild_data.get("description")}
)

Player.objects.get_or_create(
nickname=nickname,
defaults={
"email": data.get("email"),
"bio": data.get("bio"),
"race": race,
"guild": guild,
}
)


if __name__ == "__main__":
Expand Down
Loading