數(shù)據(jù)Model
#增加應(yīng)用模塊
python manage.py startapp modelname
在模塊中的model.py文件中添加相應(yīng)的model class,例如下面的model文件
from django.db import models
# Create your models here.
class Goods(models.Model):
title = models.CharField(max_length=200)
price = models.IntegerField(default=0)
create_date = models.DateTimeField(auto_now_add=True)
edit_date = models.DateTimeField(auto_now_add=True)
關(guān)于 models.Model Filed
- 在settings.py文件中的 INSTALLED_APPS 中加入剛才創(chuàng)建的model
- 使用python manage.py syncdb 就會(huì)同步該model對(duì)應(yīng)的數(shù)據(jù)庫(kù)表
遷移 Migrations
新版本的django增加了一個(gè)migrations邏輯。 syncdb過(guò)后,再次修改model的數(shù)據(jù)庫(kù)同步,叫做migrations,也就是數(shù)據(jù)庫(kù)遷移。
會(huì)在model文件夾下面生成一個(gè)migrations文件夾。syncdb后對(duì)model的修改,使用syncdb不會(huì)同步新的model修改,需要使用以下命令來(lái)操作。
python manage.py makemigrations #會(huì)在migration目錄中生成新的同步sql
python manage.py syncdb #在makemigration后調(diào)用的syncdb就會(huì)同步這次版本升級(jí)的數(shù)據(jù)庫(kù)表結(jié)構(gòu)
admin同步
如果model下面的數(shù)據(jù)需要django默認(rèn)的管理后臺(tái)來(lái)管理的話。需要在model下面的admin.py文件添加如下代碼
from django.contrib import admin
from api.models import Model
admin.site.register(Model)