Django2 +MySQL8 简单实现数据的增删改查功能

1、项目结构:

Django2 +MySQL8 简单实现数据的增删改查功能_第1张图片

blog应用的数据库模型源码:

# This is an auto-generated Django model module.
# You'll have to do the following manually to clean this up:
#   * Rearrange models' order
#   * Make sure each model has one field with primary_key=True
#   * Make sure each ForeignKey has `on_delete` set to the desired behavior.
#   * Remove `managed = False` lines if you wish to allow Django to create, modify, and delete the table
# Feel free to rename the models, but don't rename db_table values or field names.
from django.db import models


class BizArticle(models.Model):
    id = models.BigAutoField(primary_key=True)
    title = models.CharField(max_length=255, blank=True, null=True)
    user_id = models.BigIntegerField()
    cover_image = models.CharField(max_length=255, blank=True, null=True)
    qrcode_path = models.CharField(max_length=255, blank=True, null=True)
    is_markdown = models.PositiveIntegerField(blank=True, null=True)
    content = models.TextField(blank=True, null=True)
    content_md = models.TextField(blank=True, null=True)
    top = models.IntegerField(blank=True, null=True)
    type_id = models.BigIntegerField()
    status = models.PositiveIntegerField(blank=True, null=True)
    recommended = models.PositiveIntegerField(blank=True, null=True)
    original = models.PositiveIntegerField(blank=True, null=True)
    description = models.CharField(max_length=300, blank=True, null=True)
    keywords = models.CharField(max_length=200, blank=True, null=True)
    comment = models.PositiveIntegerField(blank=True, null=True)
    create_time = models.DateTimeField(blank=True, null=True)
    update_time = models.DateTimeField(blank=True, null=True)

    class Meta:
        managed = False
        db_table = 'biz_article'


class BizArticleLook(models.Model):
    id = models.BigAutoField(primary_key=True)
    article_id = models.BigIntegerField()
    user_id = models.BigIntegerField(blank=True, null=True)
    user_ip = models.CharField(max_length=50, blank=True, null=True)
    look_time = models.DateTimeField(blank=True, null=True)
    create_time = models.DateTimeField(blank=True, null=True)
    update_time = models.DateTimeField(blank=True, null=True)

    class Meta:
        managed = False
        db_table = 'biz_article_look'


class BizArticleLove(models.Model):
    id = models.BigAutoField(primary_key=True)
    article_id = models.BigIntegerField()
    user_id = models.BigIntegerField(blank=True, null=True)
    user_ip = models.CharField(max_length=50, blank=True, null=True)
    love_time = models.DateTimeField(blank=True, null=True)
    create_time = models.DateTimeField(blank=True, null=True)
    update_time = models.DateTimeField(blank=True, null=True)

    class Meta:
        managed = False
        db_table = 'biz_article_love'


class BizArticleTags(models.Model):
    id = models.BigAutoField(primary_key=True)
    tag_id = models.BigIntegerField()
    article_id = models.BigIntegerField()
    create_time = models.DateTimeField(blank=True, null=True)
    update_time = models.DateTimeField(blank=True, null=True)

    class Meta:
        managed = False
        db_table = 'biz_article_tags'


class BizComment(models.Model):
    id = models.BigAutoField(primary_key=True)
    sid = models.BigIntegerField(blank=True, null=True)
    user_id = models.BigIntegerField(blank=True, null=True)
    pid = models.BigIntegerField(blank=True, null=True)
    qq = models.CharField(max_length=13, blank=True, null=True)
    nickname = models.CharField(max_length=13, blank=True, null=True)
    avatar = models.CharField(max_length=255, blank=True, null=True)
    email = models.CharField(max_length=100, blank=True, null=True)
    url = models.CharField(max_length=200, blank=True, null=True)
    status = models.CharField(max_length=9, blank=True, null=True)
    ip = models.CharField(max_length=64, blank=True, null=True)
    lng = models.CharField(max_length=50, blank=True, null=True)
    lat = models.CharField(max_length=50, blank=True, null=True)
    address = models.CharField(max_length=100, blank=True, null=True)
    os = models.CharField(max_length=64, blank=True, null=True)
    os_short_name = models.CharField(max_length=10, blank=True, null=True)
    browser = models.CharField(max_length=64, blank=True, null=True)
    browser_short_name = models.CharField(max_length=10, blank=True, null=True)
    content = models.CharField(max_length=2000, blank=True, null=True)
    remark = models.CharField(max_length=100, blank=True, null=True)
    support = models.PositiveIntegerField(blank=True, null=True)
    oppose = models.PositiveIntegerField(blank=True, null=True)
    create_time = models.DateTimeField(blank=True, null=True)
    update_time = models.DateTimeField(blank=True, null=True)

    class Meta:
        managed = False
        db_table = 'biz_comment'


class BizTags(models.Model):
    id = models.BigAutoField(primary_key=True)
    name = models.CharField(max_length=50)
    description = models.CharField(max_length=100, blank=True, null=True)
    create_time = models.DateTimeField(blank=True, null=True)
    update_time = models.DateTimeField(blank=True, null=True)

    class Meta:
        managed = False
        db_table = 'biz_tags'


class BizType(models.Model):
    id = models.BigAutoField(primary_key=True)
    pid = models.BigIntegerField(blank=True, null=True)
    name = models.CharField(max_length=50, blank=True, null=True)
    description = models.CharField(max_length=200, blank=True, null=True)
    sort = models.IntegerField(blank=True, null=True)
    icon = models.CharField(max_length=100, blank=True, null=True)
    available = models.PositiveIntegerField(blank=True, null=True)
    create_time = models.DateTimeField(blank=True, null=True)
    update_time = models.DateTimeField(blank=True, null=True)

    class Meta:
        managed = False
        db_table = 'biz_type'


class SysConfig(models.Model):
    home_desc = models.CharField(max_length=255, blank=True, null=True)
    home_keywords = models.CharField(max_length=255, blank=True, null=True)
    domain = models.CharField(max_length=255, blank=True, null=True)
    cms_url = models.CharField(max_length=255, blank=True, null=True)
    site_url = models.CharField(max_length=255, blank=True, null=True)
    site_name = models.CharField(max_length=255, blank=True, null=True)
    site_desc = models.CharField(max_length=255, blank=True, null=True)
    site_favicon = models.CharField(max_length=255, blank=True, null=True)
    static_web_site = models.CharField(max_length=255, blank=True, null=True)
    author_name = models.CharField(max_length=100, blank=True, null=True)
    author_email = models.CharField(max_length=100, blank=True, null=True)
    wx_code = models.CharField(max_length=255, blank=True, null=True)
    qq = models.CharField(max_length=255, blank=True, null=True)
    weibo = models.CharField(max_length=255, blank=True, null=True)
    github = models.CharField(max_length=255, blank=True, null=True)
    maintenance = models.PositiveIntegerField(blank=True, null=True)
    maintenance_data = models.DateTimeField(blank=True, null=True)
    comment = models.PositiveIntegerField(blank=True, null=True)
    qiuniu_base_path = models.CharField(max_length=255, blank=True, null=True)
    qiniu_access_key = models.CharField(max_length=100, blank=True, null=True)
    qiniu_secret_key = models.CharField(max_length=100, blank=True, null=True)
    qiniu_bucket_name = models.CharField(max_length=100, blank=True, null=True)
    baidu_push_token = models.CharField(max_length=100, blank=True, null=True)
    wx_praise_code = models.CharField(max_length=200, blank=True, null=True)
    zfb_praise_code = models.CharField(max_length=200, blank=True, null=True)
    baidu_api_ak = models.CharField(max_length=100, blank=True, null=True)
    create_time = models.DateTimeField(blank=True, null=True)
    update_time = models.DateTimeField(blank=True, null=True)

    class Meta:
        managed = False
        db_table = 'sys_config'


class SysPermission(models.Model):
    id = models.BigAutoField(primary_key=True)
    name = models.CharField(max_length=200)
    description = models.CharField(max_length=200, blank=True, null=True)
    url = models.CharField(max_length=200)
    pid = models.BigIntegerField(blank=True, null=True)

    class Meta:
        managed = False
        db_table = 'sys_permission'


class SysPermissionRole(models.Model):
    id = models.BigAutoField(primary_key=True)
    role_id = models.BigIntegerField()
    permission_id = models.BigIntegerField()

    class Meta:
        managed = False
        db_table = 'sys_permission_role'


class SysRole(models.Model):
    id = models.BigAutoField(primary_key=True)
    name = models.CharField(max_length=200)

    class Meta:
        managed = False
        db_table = 'sys_role'


class SysRoleUser(models.Model):
    id = models.BigAutoField(primary_key=True)
    sys_user_id = models.BigIntegerField()
    sys_role_id = models.BigIntegerField()

    class Meta:
        managed = False
        db_table = 'sys_role_user'


class SysUser(models.Model):
    id = models.BigAutoField(primary_key=True)
    username = models.CharField(max_length=200)
    password = models.CharField(max_length=200)

    class Meta:
        managed = False
        db_table = 'sys_user'


class TCategory(models.Model):
    name = models.CharField(max_length=50)

    class Meta:
        managed = False
        db_table = 't_category'


class TCategoryItem(models.Model):
    category = models.ForeignKey(TCategory, models.DO_NOTHING, blank=True, null=True)
    item = models.ForeignKey('TItem', models.DO_NOTHING, blank=True, null=True)

    class Meta:
        managed = False
        db_table = 't_category_item'


class TItem(models.Model):
    name = models.CharField(max_length=50)

    class Meta:
        managed = False
        db_table = 't_item'

blog应用的功能函数views.py

from django.http import HttpResponse
from blog.models import TItem


def welcome(request):
    list = TItem.objects.all()
    for i in range(len(list)):
        print("序号:%s   值:%s" % (i + 1, list[i]))
    return HttpResponse("

Welcome to Python3") def insert(request): item = TItem(name='5') item.save() return HttpResponse('数据插入成功') def update(request): item = TItem.objects.get(id=1) item.name = '10' item.save() return HttpResponse("数据更新成功") def delete(request): item = TItem.objects.get(id=2) item.delete() return HttpResponse('数据删除成功') def select(request): list = TItem.objects.all() for i in range(len(list)): print("序号:%s 值:%s" % (i + 1, list[i].id)) return HttpResponse('数据查询成功')

blog 应用的路由配置urls.py

from django.conf.urls import url
from . import views

urlpatterns = [
    url(r'^insert/', views.insert),
    url(r'^delete/', views.delete),
    url(r'^update/', views.update),
    url(r'^select/', views.select),
]

效果展示:

Django2 +MySQL8 简单实现数据的增删改查功能_第2张图片

你可能感兴趣的:(python,学习笔记)