Django中创建表, 用的django项目自带的sqlite数据库,创建完成后将表注册到jdango.admin,就可以在浏览器在管理了。
在django项目的models.py文件中:
from django.db import models
# Create your models here.
class Author(models.Model):
name = models.CharField(max_length=100)
age = models.IntegerField(default=18)
class Article(models.Model):
title = models.CharField(max_length=200)
content = models.TextField()
url = models.URLField()
portal = models.ImageField()
author = models.ForeignKey(Author)
Python manage.py makemigrations
用sqlite查看是否真的创建表成功:
Python manage.py createsuperuser
或者
Pycharm
Run-->Run manage.py Task...-->Syncdb
在浏览器查看
在admin.py中加入如下代码:
from django.contrib import admin
from blog.models import *
# Register your models here.
admin.site.register(Article)
admin.site.register(Author)
用户名:adminstrator
密码:0
登录成功!