(Python3) Django:入门

1、文档教程链接:

https://docs.djangoproject.com/en/2.2/

2、下载命令:

pip3 install django

3、初识Django

3.1 设计模型:

Django无需数据库就可以使用,它提供了对象关系映射器,通过此技术,你可以使用Python代码来描述数据库结构。你可以使用强大的数据-模型语句来描述你的数据模型。

mysite/news/models.py¶

from django.db import models

class Reporter(models.Model):
    full_name = models.CharField(max_length=70)

    def __str__(self):
        return self.full_name

class Article(models.Model):
    pub_date = models.DateField()
    headline = models.CharField(max_length=200)
    content = models.TextField()
    reporter = models.ForeignKey(Reporter, on_delete=models.CASCADE)

    def __str__(self):
        return self.headline

3.2 应用数据模型

运行Django命令行工具来创建数据库表

$ python manage.py migrate

接下来,你就可以使用一套便捷而丰富的PythonAPI访问你的数据,这些API是即时创建的,而不用显式的生成代码

# Import the models we created from our "news" app
>>> from news.models import Article, Reporter

# No reporters are in the system yet.
>>> Reporter.objects.all()
<QuerySet []>

# Create a new Reporter.
>>> r = Reporter(full_name='John Smith')

# Save the object into the database. You have to call save() explicitly.
>>> r.save()

# Now it has an ID.
>>> r.id
1
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值