[py]django的manytomany字段和后台搜索过滤功能⽂章-属于某个分类-拥有多个标签: foreignkey&manytomany
看下models的manytomany和foreignkey
stratproject learnapi
startapp app01
models.py
class Category(models.Model):
name = models.CharField(max_length=30, verbose_name="类名") #修改字段名显⽰
creat_time = models.DateTimeField(auto_now_add=True, verbose_name="添加时间")
class Meta:#表名显⽰
verbose_name = "分类"
verbose_name_plural = verbose_name
def __str__(self): #xxx添加成功
return u'%s' % self.name
class Tag(models.Model):
name = models.CharField(max_length=30, verbose_name="标签名")
creat_time = models.DateTimeField(auto_now_add=True, verbose_name="添加时间")
class Meta:
verbose_name = "标签"
verbose_name_plural = verbose_name
def __str__(self):
return u'%s' % self.name
class Post(models.Model):
django前端模板
title = models.CharField(max_length=255, verbose_name="标题")
slug = models.SlugField(max_length=300, allow_unicode=True, unique=True, verbose_name="⼩标题")
content = models.TextField(verbose_name="⽂章内容")
publish_time = models.DateTimeField(auto_now_add=True, verbose_name="发布时间")
category = models.ForeignKey(Category, on_delete=models.CASCADE, verbose_name="分类")
tag = models.ManyToManyField(Tag, blank=True, verbose_name="标签")
class Meta:
verbose_name = "⽂章"
verbose_name_plural = verbose_name
def __str__(self):
return u'%s' % self.title
修改app名显⽰
app名-table名-字段名
- app01/__init__.py
default_app_config = "app01.apps.App01Config"
- apps.py
class App01Config(AppConfig):
name = 'app01'
verbose_name = u"⽂章"
注册app下的table到后台
list_display list_filter search_fields
ib import admin
from .models import Category, Tag, Post
# Register your models here.
class CategoryAdmin(admin.ModelAdmin):
list_display = ('name', 'creat_time') #后台展⽰这些列
class TagAdmin(admin.ModelAdmin):
list_display = ('name', 'creat_time')
class PostAdmin(admin.ModelAdmin):
list_display = ('title', 'publish_time', 'category') #后台展⽰这些列
list_filter = ('category',) # 分类筛选
search_fields = ('title', 'slug', 'content',)#后台索引这些字段搜索
#注册app
ister(Category, CategoryAdmin)
ister(Tag, TagAdmin)
ister(Post, PostAdmin)
todo:
select_related和prefetch_related的区别
譬如 course⾥有多个章节, ⼀个章节lesson⾥有多个,⼀个lesson⾥有多个video
from datetime import datetime
from django.db import models
# Create your models here.
class Courses(models.Model):
name = models.CharField(max_length=50, verbose_name="课程名")
desc = models.CharField(max_length=300, verbose_name="课程描述")
detail = models.TextField(verbose_name="课程详情")
degree = models.CharField(choices=(("cj", "初级"), ('ZJ', "中级"), ("GJ", "⾼级")), verbose_name="课程等级")
learn_times = models.IntegerField(default=0, verbose_name="学习时长(分钟)")
students = models.IntegerField(default=0, verbose_name="学习⼈数")
fav_nums = models.IntegerField(default=0, verbose_name="收藏⼈数")
image = models.ImageField(max_length=100, upload_to="courses/%Y/%m/", verbose_name="封⾯图")
click_nums = models.IntegerField(default=0, verbose_name="点击数")
add_time = models.DateTimeField(w, verbose_name="添加时间")
class Meta:
verbose_name = "课程"
verbose_name_plural = verbose_name
class Lesson(models.Model):
course = models.ForeignKey(Courses, verbose_name="课程")  # 看在哪个表添加外键,就看我需要你,就在我⾥⾯添加ForeignKey    name = models.CharField(max_length=100, verbose_name="章节名")
add_time = models.DateTimeField(w, verbose_name="添加时间")
class Meta:
verbose_name = "章节"
verbose_name_plural = verbose_name
class Video(models.Model):
lesson = models.ForeignKey(Lesson, verbose_name="章节")
name = models.CharField(max_length=100, verbose_name="章节名称")
add_time = models.DateTimeField(w, verbose_name="添加时间")
class Meta:
verbose_name = "视频"
verbose_name_plural = verbose_name
class CourseResourse(models.Model):
course = models.ForeignKey(Courses, verbose_name="课程名")
name = models.CharField(max_length=100, verbose_name="资源名称")
download = models.FileField(upload_to="course/resource/%Y/%m", verbose_name="资源⽂件下载地址", max_length=100)
add_time = models.DateTimeField(w, verbose_name="添加时间")
class Meta:
verbose_name = "课程资源"
verbose_name_plural = verbose_name
城市-课程机构-教师
两种为模型添加时间字段的⽅法
- ⽅法1,⽤⾃带的auto_now_add
class Category(models.Model):
name = models.CharField(max_length=20, verbose_name="分类")
create_time = models.DateTimeField(auto_now_add=True, verbose_name="添加时间")
- ⽅法2,⽤py模块
class Article(models.Model):
title = models.CharField(max_length=50, verbose_name="标题")
content = models.TextField(verbose_name="内容")
add_time = models.DateTimeField(w, verbose_name="添加时间")
继续定制admin页⾯
list_editable: 修改数据
class PostAdmin(admin.ModelAdmin):
list_display = ['title', 'category', 'add_time', ]
search_fields = ['title', 'category', ]
list_filter = ['title', 'category', 'add_time', ]
list_display_links = None  # 必须有这个,否则报错: # which cannot be used unless 'list_display_links' is set.    list_editable = ['title', 'category', ]
加上后⽅便编辑数据了:
filter_horizontal = ['tag', ]
⽀持分页了
class PostAdmin(admin.ModelAdmin):
list_display = ['title', 'category', 'add_time', ]
search_fields = ['title', 'category', ]
list_filter = ['title', 'category', 'add_time', ]
list_display_links = None
list_editable = ['title', 'category', ]
filter_horizontal = ['tag', ]
list_per_page = 2
⽀持排序了
class PostAdmin(admin.ModelAdmin):
list_display = ['title', 'category', 'add_time', ]
search_fields = ['title', 'category', ]
list_filter = ['title', 'category', 'add_time', ]
list_display_links = None
list_editable = ['title', 'category', ]
filter_horizontal = ['tag', ]
list_per_page = 2
ordering = ('-add_time',)  # 这⾥我逆序排列

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。