1在models.py中修改
from django.db importmodels
# Create your modelshere.
classHost(models.Model):
hostname =models.CharField(max_length=30)
address =models.CharField(max_length=17)
2 同步数据库
[[email protected]]# python manage.py makemigrations
Migrations for ‘blog‘:
0001_initial.py:
- Create model Host
[[email protected]]# python manage.py migrate
3 创建对象
python manage.py shell
from blog.models importHost
Host.objects.create(hostname=‘host1‘,address=‘192.168.1.101‘)
数据库查数据
sqlite> select *from blog_host;
1|host1|192.168.1.101
第二种插数据的方法
host = Host()
host.hostname=‘host2‘
host.address=‘192.168.1.102‘
host.save()
时间: 2024-10-10 22:56:52