Django DB最佳实践

时间:2011-01-18 03:55:25

标签: python django database-design

我正在与Django构建一个在线角色扮演游戏。我想知道什么是最好的方式来编写模型,以免在未来给我带来痛苦,这是我到目前为止所做的:

class Equipment(models.Model):
    head = models.ForeignKey("InvSlot",null=True,blank=True,related_name="head")
    chest = models.ForeignKey("InvSlot",null=True,blank=True,related_name="chest")
    lwep = models.ForeignKey("InvSlot",null=True,blank=True,related_name="lwep")
    rwep = models.ForeignKey("InvSlot",null=True,blank=True,related_name="rwep")
    gloves = models.ForeignKey("InvSlot",null=True,blank=True,related_name="gloves")
    acc1 = models.ForeignKey("InvSlot",null=True,blank=True,related_name="acc1")
    acc2 = models.ForeignKey("InvSlot",null=True,blank=True,related_name="acc2")
    legs = models.ForeignKey("InvSlot",null=True,blank=True,related_name="legs")
    feet = models.ForeignKey("InvSlot",null=True,blank=True,related_name="feet")


class Item(models.Model):
    name = models.CharField(max_length=200, unique=True)
    desc = models.TextField()
    image = models.TextField()
    slot_choices = ((0,"stackable"),
                (1,"head"),
                (2,"chest"),
                (3,"legs"),
                (4,"feet"),
                (5,"gloves"),
                (6,"weapon"),
                (7,"accessory"),
                (8,"noequip/stack"))
    slot = models.IntegerField(max_length=2, choices=slot_choices)
    twohand = models.BooleanField(default=0)

    def __unicode__(self):
        return self.name

class CharacterClass(models.Model):
    name = models.CharField(max_length=200)
    attack = models.IntegerField(max_length=11,default=0)
    defense = models.IntegerField(max_length=11,default=0)
    mattack = models.IntegerField(max_length=11,default=0)
    mdefense = models.IntegerField(max_length=11,default=0)
    hp = models.IntegerField(max_length=11,default=0)
    mp = models.IntegerField(max_length=11,default=0)
    luck = models.IntegerField(max_length=11,default=0)

    def __unicode__(self):
        return self.name

class InvSlot(models.Model):
# equippable weapons do not stack in inventory. Can also be enchanted
    character = models.ForeignKey("Character")
    quantity = models.IntegerField(max_length=2)
    item = models.ForeignKey("Item")
    enchant = models.ForeignKey("Buff", blank=True, null=True)
    equipped = models.BooleanField(default=0)

    def __unicode__(self):
        return self.item.name + " x " + str(self.quantity)

class Character(models.Model):
    fbid = models.CharField(unique=True, max_length=200)
    name = models.CharField(unique=True, max_length=200)
    char_class = models.ForeignKey(CharacterClass)
    inventory = models.ManyToManyField("InvSlot",null=True,blank=True, related_name="inventory")
    equipment = models.ForeignKey(Equipment,null=True,blank=True)
    buffs = models.ManyToManyField("Buff",null=True,blank=True, related_name="buffs")
    exp = models.IntegerField(max_length=11,default=0)
    attpoints = models.IntegerField(max_length=11,default=0)
    money = models.IntegerField(max_length=11,default=0)
    energy = models.IntegerField(max_length=11,default=0)
    GENDER_CHOICES = (
    ('M', 'Male'),
    ('F', 'Female'),
    )
    gender = models.CharField(max_length=1, choices=GENDER_CHOICES)
    lastactive = models.DateTimeField(default=datetime.now)
    level = models.IntegerField(max_length=11,default=0)
    attack = models.IntegerField(max_length=11,default=0)
    defense = models.IntegerField(max_length=11,default=0)
    mattack = models.IntegerField(max_length=11,default=0)
    mdefense = models.IntegerField(max_length=11,default=0)
    hp = models.IntegerField(max_length=11,default=0)
    hpmax = models.IntegerField(max_length=11,default=0)
    mp = models.IntegerField(max_length=11,default=0)
    mpmax = models.IntegerField(max_length=11,default=0)
    luck = models.IntegerField(max_length=11,default=0)

    def updateActive(self):
        self.date_modified = datetime.now
        super(Character, self).save()

    def __unicode__(self):
        return self.name

class CharacterBuff(models.Model):
    character = models.ForeignKey("Character")
    buff = models.ForeignKey("Buff")
    until = models.DateTimeField()

    def __unicode__(self):
        return self.buff.name + " on " + self.character.name

class Buff(models.Model):
# can be used on both players and items
    name = models.CharField(unique=True, max_length=200)
    attack = models.IntegerField(max_length=11)
    defense = models.IntegerField(max_length=11)
    mattack = models.IntegerField(max_length=11)
    mdefense = models.IntegerField(max_length=11)
    hp = models.IntegerField(max_length=11)
    # can only be used on players
    posion = models.BooleanField(default=0)
    sleep = models.BooleanField(default=0)
    burned = models.BooleanField(default=0)

    def __unicode__(self):
        return self.name

我是一个db菜鸟,我真的想要一些指示,如果有更好的方法来设置它。

1 个答案:

答案 0 :(得分:1)

您可能希望查看像South这样的数据库迁移工具。这将有助于在部署应用程序后更新数据库更新的痛苦,并且新版本需要更改模型。