创建models

在my_blog/article/models.py下编写如下程序:

  1. from django.db import models
  2. # Create your models here.
  3. class Article(models.Model) :
  4. title = models.CharField(max_length = 100) #博客题目
  5. category = models.CharField(max_length = 50 blank = True) #博客标签
  6. date_time = models.DateTimeField(auto_now_add = True) #博客日期
  7. content = models.TextField(blank = True null = True) #博客文章正文
  8. def __unicode__(self) :
  9. return self.title
  10. class Meta: #按时间下降排序
  11. ordering = ['-date_time']

其中unicode(self) 函数Article对象要怎么表示自己, 一般系统默认<Article: Article object>使用来表示对象, 通过这个函数可以告诉系统使用title字段来表示这个对象。

  • CharField 用于存储字符串, max_length设置最大长度
  • TextField 用于存储大量文本
  • DateTimeField 用于存储时间, auto_now_add设置True表示自动设置对象增加时间