美文网首页
django创建模型

django创建模型

作者: 寻找无名的特质 | 来源:发表于2022-06-04 06:43 被阅读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,在数据库中创建了表。

相关文章

网友评论

      本文标题:django创建模型

      本文链接:https://www.haomeiwen.com/subject/vbcxmrtx.html