Models

  • model名采用大驼峰命名法
  • 字段名全部使用小写字母加下划线格式
  • 必须添加主键,且主键必须是UUIDField字段
  • 值为多选一的字段,必须添加choices参数,且必须使用大写
  • 有需要作为搜索条件的字段都需要加上索引
  • class Meta必须位于字段定义完成之后,其他函数之前
  • 必须添加这些字段created_timeupdated_timedeleteddeleted_timedescription
  • model类中的各成员的顺序应遵循以下顺序(不是所有项都是必须的)
    1. 所有的数据库字段
    2. 自定义的管理器属性(django1.9)
    3. class Meta
    4. def __unicode__()
    5. def __str__()
    6. 需要重写的models.Model函数
    7. 自定义的property函数
    8. def brief_info()
    9. def detail_info()
    10. 其他自定义函数
class Person(models.Model):
    person_id = models.UUIDField(primary_key=True, default=uuid.uuid1, db_index=True)
    first_name = models.CharField(max_length=20, db_index=True)
    last_name = models.CharField(max_length=40, db_index=True)
    GENDERS = (
        ('MALE', 'MALE'),
        ('FEMALE', 'FEMALE'),
    )
    gender = models.CharField(max_length=10, choices=GENDERS, db_index=True)
    birthday = models.DateTimeField(null=True, db_index=True)
    job = models.CharField(max_length=200, blank=True, db_index=True)
    created_time = models.DateTimeField(auto_now_add=True, db_index=True)
    updated_time = models.DateTimeField(auto_now=True, db_index=True)
    deleted = models.BooleanField(default=False, db_index=True)
    deleted_time = models.DateTimeField(null=True)
    description = models.TextField(blank=True)

    class Meta:
        db_table = 'person'

    def brief_info(self):
        return {
            'person_id': self.person_id.hex,
            'first_name': self.first_name,
            'last_name': self.last_name,
        }

    def detail_info(self):
        return {
            'person_id': self.person_id.hex,
            'first_name': self.first_name,
            'last_name': self.last_name,
            'gender': self.gender,
            'birthday': self.birthday.timestamp(),
            'job': self.job,
            'created_time': self.created_time.timestamp(),
            'updated_time': self.updated_time.timestamp(),
            'deleted': self.deleted,
            'deleted_time': self.deleted_time.timestamp(),
            'description': self.description,
        }

results matching ""

    No results matching ""