django创建模型
2022-06-04 本文已影响0人
寻找无名的特质
在app的models.py中创建模型,比如,计算学生的BMI:
from django.db import models
class Student(models.Model):
name_text=models.CharField(max_length=20)
height=models.DecimalField(max_digits=10,decimal_places=3)
weight=models.DecimalField(max_digits=10,decimal_places=3)
创建完成后,需要在项目的settings.py中注册应用:
INSTALLED_APPS = [
'myfirst.apps.MyfirstConfig',
然后需要运行python manage.py makemigrations myfirst,生成迁移文件。我们可以用命令python manage.py sqlmigrate myfirst 0001查看生成的sql语句。
然后执行python manage.py migrate,在数据库中创建了表。