前言
通过上一篇文章: wordpress直接通过数据库导出文章标题、分类信息到xls表格 ,将数据导入到excel表格中了。在处理的过程中发现仍然不是很方便,一是数据量有点大,每次打开更新需要的的时间有点久。另外无法统计分析。于是想把它输入到自己的数据库中,方便处理查看查询。
Django添加新模块resource
from django.db import models
#状态
U = 0
Y = 1
N = 2
STATUS_CHOICE = {
U: '未处理',
Y: '可用',
N: '不可用',
}
# Create your models here.
class Resource(models.Model):
'''
任务模型类
'''
id = models.AutoField(primary_key=True)
rid = models.CharField(max_length=16, verbose_name='编号', default='0')
rtitle = models.CharField(max_length=256, verbose_name='标题')
rcontent = models.TextField(max_length=2048*8, verbose_name='说明')
rparent1 = models.CharField(max_length=128, verbose_name='一级分类', default='0')
rparent2 = models.CharField(max_length=128, verbose_name='二级分类', default='1')
rurl = models.CharField(max_length=256, verbose_name='下载链接', default='0')
rpwd = models.CharField(max_length=256, verbose_name='解压密码', default='0')
status_choices = ((k, v) for k,v in STATUS_CHOICE.items())
rstatus = models.SmallIntegerField(default='0', choices=status_choices, verbose_name='状态')
# admin显示订单的id
def __str__(self):
return self.rtitle
class Meta:
db_table = 'code_resource'
verbose_name = '源码资源'
verbose_name_plural = '源码资源'
# 修改项目settings.py,添加模块
INSTALLED_APPS = [
...
'resource',
...
],
# 更新
python3 manage.py makemigrations
python3 manage.py migrate
show create table code_resource;
待处理资源wordpress数据库操作
CREATE TABLE `code_resource` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`rid` varchar(16) COLLATE utf8_bin NOT NULL,
`rtitle` varchar(256) COLLATE utf8_bin NOT NULL,
`rcontent` longtext COLLATE utf8_bin NOT NULL,
`rparent1` varchar(128) COLLATE utf8_bin NOT NULL,
`rparent2` varchar(128) COLLATE utf8_bin NOT NULL,
`rurl` varchar(256) COLLATE utf8_bin NOT NULL,
`rpwd` varchar(256) COLLATE utf8_bin NOT NULL,
`rstatus` smallint(6) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=60031 DEFAULT CHARSET=utf8 COLLATE=utf8_bin
insert into code_resource(rid, rtitle, rcontent, rparent1, rparent2, rurl, rpwd, rstatus)
select
a.ID,
a.post_title,
a.post_content,
(select name from wp_terms where term_id = b.parent) as parent1,
c.name as sparent2,
ifnull((select meta_value from wp_postmeta where post_id = a.ID and meta_key = '_riplus_down_info'), 'x') as resource,
ifnull((select meta_value from wp_postmeta where post_id = a.ID and meta_key = '_riplus_other_info'), 'x') as pwd,
0 as rstatus
from wp_term_taxonomy b
join wp_terms c
on b.term_id = c.term_id
join wp_term_relationships d
on b.term_taxonomy_id = d.term_taxonomy_id
join wp_posts a
on a.ID = d.object_id
where b.taxonomy = 'category'
and b.parent != 0;
# 导出
mysqldump -uroot -pxxxx -h 127.0.0.1 yonp code_resource > code_resource.sql
# 导入
mysql -uroot -pxxxx taskmonitor < code_resource.sql
数据展示
from django.contrib import admin
from resource.models import Resource
from django.utils.html import format_html
# Register your models here.
class ResourceAdmin(admin.ModelAdmin):
#后台展示字段
list_display = ['rid', 'rtitle','rparent1', 'rparent2', 'down_url', 'down_pwd', 'untar_pwd', 'rstatus']
#过滤字段
list_filter = ["rparent1", "rparent2", "rstatus"]
#搜索字段
search_fields = ['rtitle']
def down_url(self, obj):
"""
下载链接
"""
tmp_li = obj.rurl.split('"')
dname = '未知'
durl = '/'
dpwd = 'x'
if len(tmp_li) > 11:
dname = tmp_li[3]
durl = tmp_li[7]
dpwd = tmp_li[11]
return format_html("<a href='{u}' target='_blank'>{n}</a>", u=durl, n=dname, p=dpwd)
def down_pwd(self, obj):
"""
访问密码
"""
tmp_li = obj.rurl.split('"')
dpwd = obj.rid
if len(tmp_li) > 11:
dpwd = tmp_li[11]
//点击复制功能
return format_html(f"""
<input type="text" id="{dpwd}" value="{dpwd}" style="position: absolute; top: -10000px">
<a href="#" onclick="document.getElementById('{dpwd}').select(); document.execCommand('copy')">{dpwd}</a>
""")
def untar_pwd(self, obj):
"""
压缩包解压密码
"""
if obj.rpwd != 'x':
tmp_li = obj.rpwd.split('"')
return_str = tmp_li[7]
else:
return_str = obj.rpwd
return return_str
down_url.short_description = '下载链接'
down_pwd.short_description = '访问密码'
untar_pwd.short_description = '解压密码'
admin.site.register(Resource, ResourceAdmin)
FAQ
django 数据库更新抱错后,重新执行
# 确保存在文件
resource/migrations/__init__.py
# 登录数据库,清理resource模块记录
select * from django_migrations;
评论 (0)