Django模型设计

时间:2015-08-12 15:45:21

标签: python django django-models

我对django不熟悉,需要django模型的帮助。

我需要在django的sqlite数据库中存储以下类型的记录。 如何设计可存储此数据的模型。

我可以使用SmallIntegerField存储Room_no和CharField来存储Student_name。

但我不确定如何存储Sports数组。我想以这样的方式存储它,如果我必须列出所有学生的任何运动,查询数据库应该是有效的。

  {
    "Room_no": 2,
    "Student_name": "XYZ",
    "Sports": [
      "Football",
      "Tennis",
      "Cricket"
    ]  
  }

我能想到的一种方式是:

class Sports(models.Model):
        sports = models.CharField(max_length=100)

class Student(models.Model):
        Student_name = models.CharField(max_length=255)
        Room_no = models.IntegerField()
        sports = models.ManyToManyField(Sports)

Django版本:1.8.3

1 个答案:

答案 0 :(得分:1)

以下是我如何使用python和Django标准的其他代码样式更改。

# Your models name should be singular rather than plural.
class Sport(models.Model):
        # Use name here rather than sports since it's more descriptive.
        name = models.CharField(max_length=100)

class Student(models.Model):
        # You're in the student model, it can be assumed that the name is the student's name.
        name = models.CharField(max_length=255)
        # The fields should be lower case.
        room_no = models.IntegerField()
        sports = models.ManyToManyField(Sport)
相关问题