Skip to content
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 #1016

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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
31 changes: 31 additions & 0 deletions db/models.py
Original file line number Diff line number Diff line change
@@ -1 +1,32 @@
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)

Choose a reason for hiding this comment

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

The bonus field is currently a CharField. If this field is meant to store numerical values, consider using IntegerField or FloatField instead.

Choose a reason for hiding this comment

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

The bonus field in the Skill model is defined as a CharField. If bonus is intended to store numerical values (e.g., a bonus point or percentage), consider using a numerical field type like IntegerField or FloatField instead. This will ensure data integrity and allow for numerical operations if needed.

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


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)

Choose a reason for hiding this comment

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

The bio field is a CharField with a max length of 255. If this field is intended to store longer text, consider using TextField instead.

race = models.ForeignKey(Race,
on_delete=models.CASCADE,
related_name="player_set")
guild = models.ForeignKey(Guild,
on_delete=models.SET_NULL,
null=True,
blank=True,)
created_at = models.DateTimeField(auto_now_add=True)
39 changes: 37 additions & 2 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,45 @@
from django.db.models import ForeignKey # noqa: F401
import init_django_orm # noqa: F401

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


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

for name, value in players.items():

race, _ = Race.objects.get_or_create(
name=value["race"]["name"],
description=value["race"]["description"]
)

guild = None
if value["guild"] is not None:
guild, _ = Guild.objects.get_or_create(
name=value["guild"]["name"],
description=(
value["guild"]["description"]
if value["guild"]["description"]
else None
),
)

for skill in value["race"]["skills"]:
Skill.objects.get_or_create(
name=skill["name"],
bonus=skill["bonus"],

Choose a reason for hiding this comment

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

Ensure that the bonus value from the JSON matches the data type expected by the Skill model. If the bonus field in the model is changed to a numerical type (as suggested in the models.py review), make sure to convert or validate the bonus value accordingly before saving it to the database.

race=race,
)

Player.objects.create(
nickname=name,
email=value["email"],
bio=value["bio"],
race=race,
guild=guild,
)


if __name__ == "__main__":
Expand Down
Loading