时间:2021-07-01 10:21:17 帮助过:3人阅读
把官网的那段示例Model copy过来,为了加深印象,我自己会手写一边:
from django.db import models class Blog(models.Model): name = models.CharField(max_length=100) tagline = models.TextField() def __str__(self): # __unicode__ on Python 2 return self.name class Author(models.Model): name = models.CharField(max_length=50) email = models.EmailField() def __str__(self): # __unicode__ on Python 2 return self.name class Entry(models.Model): blog = models.ForeignKey(Blog) headline = models.CharField(max_length=255) body_text = models.TextField() pub_date = models.DateField() mod_date = models.DateField() authors = models.ManyToManyField(Author) n_comments = models.IntegerField() n_pingbacks = models.IntegerField() rating = models.IntegerField() def __str__(self): # __unicode__ on Python 2 return self.headline
抄完了:
注意红框标出来的,这在官方示例中是没有的,意思是手动指定表名,如果不手动指定的话,最终生成的表名是:APPName_ModelsName,比如这里我的app名字是myLesson,Models名字为Blog,生成的表名为:myLesson_Blog
dos虚拟环境下我们来验证下有没有错误:
--先保存下
Python Django 开发 2 数据库
标签: