I'm new in Django and I have a question about architecture. I'm not looking for a ready solution, just for some explanations in a simple way, and maybe tips about architecture.
I want to create a simple classic RPG helper, and that's why a need three models:
- Fraction
- Class
- Character
And Character stats should be based on another two models. Example:
class Fraction(models.Model):
describe = models.CharField(max_length=300)
hp = models.modelIntegerField(default = 0)
dmg = models.modelFloatField(default = 0.0)
class Char_class(models.Model):
describe = models.CharField(max_length=300)
hp = models.modelIntegerField(default = 0)
dmg = models.modelFloatField(default = 0.0)
skills = models.CharField(max_length=50)
class Character(models.Model):
char_id = models.modelIntegerField(default = 0)
fraction = models.ForeignKey(Fraction, on_delete=models.DO_NOTHING)
char_class = models.ForeignKey(Char_class, on_delete=models.DO_NOTHING)
hp = model.modelIntegerField(default = fraction.hp + char_class.hp)
dmg = model.modelFloatField(default = fraction.dmg + char_class.dmg)
And what do I want to obtain?
- I thought I will just create in admin Django some class, and some fraction.
- Next after that I will create some characters just picking fractions and classes from the list, and then hp, and dmg, should be a sum of the value from that two models.
And I got the error, that ForeignKey doesn't have a dmg/hp variable. I just googled and found some information, and now if I understand correctly that approach is bad from start. If I understand correctly models are just info for Django on how to create new tables in the database, and they are not used as standard classes in python (?).
So my question is:
- How should I implement my idea in Django?
Should I create just a few models without foreign keys and somehow connect them in python code? Or maybe there is a some good practice to solve my problem using a foreign key? In the end (but I think it's not good idea), I can create one model, some char_class and fraction in two lists to pick, and then by if/elif/else just added different values for hp and dmg.