站点
django自带站点用来操作增删改查
国际化(汉化)
python
# LANGUAGE_CODE = 'en-us'
LANGUAGE_CODE = 'zh-Hans'
# TIME_ZONE = 'UTC'
TIME_ZONE = 'Asia/Shanghai'
USE_I18N = True
USE_L10N = True
USE_TZ = True
创建模型
在子应用的models.py中创建,如下:
python
from django.db import models
# Create your models here.
from django.db import models
class BookInfo(models.Model):
name = models.CharField(max_length=15)
def __str__(self):
return self.name
pass
class PeopleInfo(models.Model):
name = models.CharField(max_length=15)
gender = models.BooleanField()
book = models.ForeignKey(BookInfo,on_delete=models.CASCADE)
pass
生成迁移文件
在pycharm的终端或虚拟环境执行
bash
python manage.py makemigrations
执行迁移
在pycharm的终端或虚拟环境执行
bash
python manage.py migrate
此时会在项目的db.sqlite3中生成信息
注册模型到站点
在子应用的admin.py中注册,示例如下:
python
from django.contrib import admin
# Register your models here.
from book.models import BookInfo,PeopleInfo
admin.site.register(BookInfo)
admin.site.register(PeopleInfo)
重写__str__
目的:显示更全的数据
重写model的__str__,如:
python
from django.db import models
# Create your models here.
from django.db import models
class BookInfo(models.Model):
name = models.CharField(max_length=15)
def __str__(self):
return self.name
pass
class PeopleInfo(models.Model):
name = models.CharField(max_length=15)
gender = models.BooleanField()
book = models.ForeignKey(BookInfo,on_delete=models.CASCADE)
pass
创建超管
bash
python manage.py createsuperuser
界面访问
http://ip:port/admin