Django 数据库最佳实践
我正在用 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
我是一个数据库菜鸟,我真的很想要一些指示,如果有一个更好的方法来设置它。
I am building an online roleplaying game with Django. I am wondering what the best way to write the models in such a way as to not cause me pain in the future, Here is what I have so far:
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
I am kind of a db noob and I would really like some pointers as to if there is a better way to set this up.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可能想研究一下数据库迁移工具,例如 South。一旦部署应用程序并且新版本需要更改模型,这将有助于减轻以后数据库更新的痛苦。
You might want to look into a database migration tool like South. This will help keep down the pain of database updates later once app is deployed and a new version requires a change to the models.