添加数据
from django.db import models
Create your models here.
博客
class Blog(models.Model):
name = models.CharField(max_length=50)
tagline = models.CharField(max_length=50)
def __str__(self):
return self.name
作者
class Author(models.Model):
name = models.CharField(max_length=25)
email = models.EmailField()
def __str__(self):
return self.name
文章
class Entry(models.Model):
blog = models.ForeignKey(Blog, on_delete=models.CASCADE) # 级联删除 博客删除 文章也跟着删除
headline = models.CharField(max_length=255)
body_text = models.TextField()
authors = models.ManyToManyField(Author)
def __str__(self):
return self.body_text
文章详情
class EntryDetail(models.Model):
entry = models.OneToOneField(Entry, on_delete=models.CASCADE)
details = models.TextField()
def __str__(self):
return self.details
作者数据
from the_8.models import Author
author = Author(name='qiye', email='qiye@163.com')
author.save()
author = Author(name='lanqiao', email='lanqiao@163.com')
author.save()
博客数据
from the_8.models import Blog
blog = Blog(name='lanqiao_blog', tagline='python杀招')
blog.save()
blog = Blog(name='qiye_blog', tagline='python小技巧')
blog.save()
文章的数据
from the_8.models import Entry
el = Entry(blog=blog, headline='快速掌握python', body_text='多敲,多思考,少玩,少做梦')
entry.save()
中间表
el.authors.add(author)
author = Author.objects.get(id=1)
el.authors.add(au)
文章详情
from the_8.models import EntryDetail
ed = EntryDetail(entry=el, details='这里我省略的5000字的评论!')
ed.save()