-
Notifications
You must be signed in to change notification settings - Fork 917
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
base: master
Are you sure you want to change the base?
Solution #993
Changes from 1 commit
e92b792
e71af58
a593fe7
246cc83
9001665
e77706c
7077577
68a2e03
07b5ee4
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 |
---|---|---|
@@ -1 +1,32 @@ | ||
from django.db import models | ||
|
||
|
||
class Race(models.Model): | ||
PLAYER_CHOICES = ( | ||
("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) | ||
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. add related name for relational fields 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. 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) |
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( | ||||||
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. You could omit the second returned argument since it is not used
Suggested change
|
||||||
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__": | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,3 +18,5 @@ | |
USE_TZ = False | ||
|
||
INSTALLED_APPS = ("db",) | ||
|
||
DEBUG = True |
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.
Please, define choices correctly and use them in case of definition