django模型层

每个模型层都可以被映射为数据库中的一个数据库,而类属性被映射为数据字段,除此之外,数据表的主键、外键、约束等都可以通过属性完成定义

from django.db import models

# Create your models here.

class Category(models.Model):
    name = models.CharField(verbose_name="分类名称",max_length=20)  #=> varchar
    create_time = models.DateTimeField(verbose_name="创建时间",auto_now_add=True) #
    update_time = models.DateTimeField(verbose_name="更新时间",auto_now=True) #

    def __str__(self):
        return self.name

    class Meta:
        # db_table = "category" #实际在数据库里面创建的表名
        verbose_name = "分类表"
        verbose_name_plural = verbose_name

#python
 #10
class Article(models.Model):
    title = models.CharField(verbose_name="文章标题",max_length=50)
    desc = models.CharField(verbose_name="文章描述",max_length=100) #255
    content = models.TextField(verbose_name="文章内容") #文本类型
    img = models.ImageField(verbose_name="文章图片",null=True,blank=True,upload_to='./static') #
    show_count = models.IntegerField(verbose_name="查看次数",default=0)
    category = models.ForeignKey(Category,on_delete=models.PROTECT,verbose_name="分类")
    create_time = models.DateTimeField(verbose_name="创建时间",auto_now_add=True) #
    update_time = models.DateTimeField(verbose_name="更新时间",auto_now=True) #

    #models.DO_NOTHING  什么也不做
    #models.CASCADE  级联
    #models.SET_DEFAULT  #设置一个默认值
    #models.PROTECT 受保护的

    def __str__(self):
        return self.title

    class Meta:
        db_table = "article"
        verbose_name = "文章"
        verbose_name_plural = verbose_name





后通过 命令输入数据库


python manage.py makemigrations
python manage.py migrate

单独的python文件想要去查询数据库

import os,django
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'tqz_django.settings')
django.setup()
#指定django的配置

from user import models
#
# print( models.Category.objects.all() ) #查询所有的数据
#
# for obj in models.Category.objects.all():
#     print(obj.name)
#
# #select * from category;
#
# obj = models.Category.objects.get(id=1)  #只能返回一条数据,返回多条数据会报错
# #select * from category where id = 1;
#
# print(obj.name)
# print(obj.create_time)
# print(obj.update_time)

#基本的查询
# print(models.Article.objects.filter(category_id=2))
# print(models.Article.objects.get(category_id=2))

#新增
# models.Article.objects.create(
#     title="python123",
#     desc="这是描述",
#     content="文章内容太",
#     category_id=2
# )


# art = models.Article(title="python456",desc="描述",content="python456python456",category_id=2)
# art.save()

#修改

#单个修改
# a = models.Article.objects.get(id=1)
# a.title = "万术成要吃饭"
# a.save()
#批量修改
# models.Article.objects.filter(show_count=0).update(show_count=500)#按照条件

#models.Article.objects.all().update(show_count=500) #修改所有的


#单个删除

# a = models.Article.objects.get(id=1)
# a.delete()

# models.Article.objects.filter(show_count=0).delete()#按照条件删除
# models.Article.objects.all().delete() #删除所有的

django模型层_第1张图片

需要去添加相关代码
如果需要在admin网站上看到这些数据,需要在admin.py添加该类

from django.contrib import admin

# Register your models here.

from . import models


admin.site.register(models.Category)
admin.site.register(models.Article)

在admin后台添加数据,可以在网页看到,写for循环并且显示数据

             
                <div class="card border-0 rounded-0 px-3 mb-2 mb-md-3" id="category-card">
                    <div class="card-header bg-white px-0">
                        <strong><i class="fa fa-book mr-2 f-17">i>文章分类strong>
                    div>

                    <ul class="list-group list-group-flush f-16">

                        {% for category in categories %}
                        <li class="list-group-item d-flex justify-content-between align-items-center pr-2 py-2">
                            <a class="category-item" href="/category?id={{ category.id }}" title="查看【CI/CD】分类下所有文章">{{ category.name }}a>
                            <span class="badge text-center" title="当前分类下有6篇文章">{{ category.article_set.count }}span>
                        li>

                        {% endfor %}

                    ul>
                div>

你可能感兴趣的:(测试,测试开发,django,python,后端)