基於IMOOC強力django+殺手級xadmin 打造上線標準的線上教育平臺課程的學習(3)
建立 model
- models.py
#coding=utf-8
from django.db import models
class UserMessage(models.Model):
name = models.CharField(max_length=20, verbose_name="使用者名稱")
email = models.EmailField(verbose_name="郵箱")
address = models.CharField(max_length=100, verbose_name="聯絡地址")
message = models.CharField(max_length=500 , verbose_name="留言資訊")
class Meta:
verbose_name = "使用者留言資訊"
verbose_name
理解為對欄位的註釋
- 關於 Meta 類的一點解釋
使用內部的class Meta 定義模型的元資料
Options.verbose_name
A human-readable name for the object, singular:>
verbose_name = "pizza"
If this isn’t given, Django will use a munged version of the class name: CamelCase becomes camel case.
- 參考資料:
在資料庫中生成剛建立的 model
- 執行
migrate message
命令:
- 報錯,在
settings.py
中進行註冊:
- 再 migrate 一次:
- 使用 Navicat 檢視一下:
Djang 常用欄位型別
CharField 必須要有 max_length 引數
- 新增主鍵:
- 重新遷移一下:
- 檢查一下資料庫:
Meta 類中的其他一些內容
Options.db_table
The name of the database table to use for the model:
db_table = 'music_album'
Options.ordering The default ordering for the object, for use when obtaining lists of objects:
ordering = ['-order_date']
This is a tuple or list of strings. Each
string is a field name with an optional“-”
prefix, which indicates
descending order. Fields without a leading“-”
will be ordered
ascending. Use the string”?”
to order randomly.For example, to order by a pub_date field ascending, use this:
ordering = ['pub_date']
To order by pub_date descending, use this:
ordering = ['-pub_date']
Options.verbose_name_plural
The plural name for the object:
verbose_name_plural = "stories"
If this isn’t given, Django will use verbose_name + “s”.