0

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.

martin
  • 1,145
  • 1
  • 7
  • 24
  • Can the value of `hp` and `dmg` for a character change? And can that changed value be different from the formula you used for the default? – Abdul Aziz Barkat May 04 '22 at 18:11
  • Does this answer your question? [Model field based on other fields?](https://stackoverflow.com/questions/22157437/model-field-based-on-other-fields) – Abdul Aziz Barkat May 04 '22 at 18:13
  • Thanks for comment! About first question - I want to prepare some predefined fraction, and classes and based on it pick specific classes and fraction while I create character. Each predefined class and fraction will have own fields values, so character will be build based on it. About second comment: not exactly same problem, coz here field base on field from same model, but I will try to use property! Thanks again for answer and hints – martin May 04 '22 at 18:40

0 Answers0