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 #993

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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):
PLAYER_CHOICES = (

Choose a reason for hiding this comment

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

Please, define choices correctly and use them in case of definition

("Elf",
"Dwarf",
"Human",
"Ork")
)
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.TextField(max_length=255)
race = models.ForeignKey(Race, on_delete=models.CASCADE)

Choose a reason for hiding this comment

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

add related name for relational fields

Choose a reason for hiding this comment

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

not resolved



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.CharField(max_length=255, unique=True)
bio = models.TextField(max_length=255)
race = models.ForeignKey(Race, on_delete=models.CASCADE)
guild = models.ForeignKey(Guild, on_delete=models.DO_NOTHING)
created_at = models.DateTimeField(auto_now_add=True)
37 changes: 36 additions & 1 deletion main.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,45 @@
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 file:
players = json.load(file)
for player_name, player_data in players.items():
race_data = player_data["race"]
race, created = Race.objects.get_or_create(

Choose a reason for hiding this comment

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

You could omit the second returned argument since it is not used

Suggested change
race, created = Race.objects.get_or_create(
race, _ = Race.objects.get_or_create(

name=race_data["name"],
defaults={"description": race_data["description"]}
)
for skill_data in race_data["skills"]:
Skill.objects.get_or_create(
name=skill_data["name"],
defaults={
"bonus": skill_data["bonus"],
"race": race
}
)
guild = None
if player_data.get("guild"):
guild = player_data["guild"]
Guild.objects.get_or_create(
name=guild["name"],
defaults={
"description": guild["description"]
}
)
Player.objects.get_or_create(
nickname=player_name,
defaults={
"email": player_data["email"],
"bio": player_data["bio"],
"race": race,
"guild": guild,
"created_at": "now"
}
)


if __name__ == "__main__":
Expand Down
2 changes: 2 additions & 0 deletions settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,5 @@
USE_TZ = False

INSTALLED_APPS = ("db",)

DEBUG = True
Loading