-
Notifications
You must be signed in to change notification settings - Fork 940
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 #1037
base: master
Are you sure you want to change the base?
Solution #1037
Changes from all commits
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 |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# Generated by Django 4.0.2 on 2024-11-05 09:44 | ||
|
||
from django.db import migrations, models | ||
import django.db.models.deletion | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
initial = True | ||
|
||
dependencies = [ | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name='Guild', | ||
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, null=True)), | ||
], | ||
), | ||
migrations.CreateModel( | ||
name='Race', | ||
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='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)), | ||
('race', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='skills', to='Race')), | ||
], | ||
), | ||
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(blank=True, max_length=255)), | ||
('created_at', models.DateTimeField(auto_now_add=True)), | ||
('guild', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='members', to='Guild')), | ||
('race', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='players', to='Race')), | ||
], | ||
), | ||
] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# Generated by Django 4.0.2 on 2024-11-05 09:59 | ||
|
||
from django.db import migrations, models | ||
import django.db.models.deletion | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('db', '0001_initial'), | ||
] | ||
|
||
operations = [ | ||
migrations.AlterField( | ||
model_name='player', | ||
name='guild', | ||
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='players', to='Guild'), | ||
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. The 'to' parameter in the ForeignKey should reference the model name as a lowercase string. Change 'to='Guild'' to 'to='guild''. |
||
), | ||
migrations.AlterField( | ||
model_name='player', | ||
name='race', | ||
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='Race'), | ||
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. The 'to' parameter in the ForeignKey should reference the model name as a lowercase string. Change 'to='Race'' to 'to='race''. |
||
), | ||
migrations.AlterField( | ||
model_name='skill', | ||
name='race', | ||
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='Race'), | ||
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. The 'to' parameter in the ForeignKey should reference the model name as a lowercase string. Change 'to='Race'' to 'to='race''. |
||
), | ||
] |
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) | ||
race = models.ForeignKey(Race, on_delete=models.CASCADE) | ||
|
||
|
||
class Guild(models.Model): | ||
name = models.CharField(max_length=255, unique=True) | ||
description = models.TextField(null=True, blank=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, blank=True) | ||
race = models.ForeignKey(Race, on_delete=models.CASCADE) | ||
guild = models.ForeignKey( | ||
Guild, | ||
on_delete=models.SET_NULL, | ||
null=True, | ||
blank=True, | ||
related_name="players" | ||
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. The related_name 'players' for the ForeignKey in the Player model might cause confusion with the related_name 'members' used in the migration file for the same relationship. Ensure consistency in related_name usage across your models and migrations. |
||
) | ||
created_at = models.DateTimeField(auto_now_add=True) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,44 @@ | ||
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_data = json.load(file) | ||
|
||
for nickname, player_data in players_data.items(): | ||
race_data = player_data["race"] | ||
race, _ = Race.objects.get_or_create( | ||
name=race_data["name"], defaults={ | ||
"description": race_data.get("description", "") | ||
} | ||
) | ||
|
||
for skill_data in race_data.get("skills", []): | ||
Skill.objects.get_or_create( | ||
name=skill_data["name"], | ||
race=race, | ||
defaults={"bonus": skill_data["bonus"]} | ||
) | ||
|
||
guild = None | ||
if player_data.get("guild"): | ||
guild_data = player_data["guild"] | ||
guild, _ = Guild.objects.get_or_create( | ||
name=guild_data["name"], | ||
defaults={ | ||
"description": guild_data.get("description", "") | ||
} | ||
) | ||
|
||
Player.objects.get_or_create(nickname=nickname, defaults={ | ||
"email": player_data["email"], | ||
"bio": player_data["bio"], | ||
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. Ensure that 'bio' is provided in the player_data. If 'bio' might be missing, consider using player_data.get('bio', '') to avoid potential KeyError. |
||
"race": race, | ||
"guild": guild | ||
}) | ||
|
||
|
||
if __name__ == "__main__": | ||
|
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.
The 'to' parameter in the ForeignKey should reference the model name as a lowercase string. Change 'to='Race'' to 'to='race''.