前提ajango的 数据库主要是为了存取网站的一些内容,数据库的设置一般放在model.py 下 目录下
我们设置如下的数据库:具体的代码如下面所示:
1 # -*- coding: utf-8 -*- 2 import pdb 3 from django.db import models 4 from django.contrib.auth.models import AbstractUser 5 # Create your models here. 6 class Tag(models.Model): 7 name = models.CharField(max_length = 30,verbose_name='标签名称') 8 9 #方便在admin中查看名字 10 class Meta: 11 verbose_name = '标签' 12 verbose_name_plural = verbose_name 13 ordering = ['id'] 14 15 #调用时返回自身的一些属性,注意python3用这个,python2用__unicode__ 16 def __str__(self): 17 #必须返回字符串类型,str(self.id) 18 return self.name 19 20 # 文章分类 21 class Category(models.Model): 22 name = models.CharField(max_length=30, verbose_name='分类名称') 23 #排序属性 24 index = models.IntegerField('显示顺序(从小到大)',default=999) 25 26 class Meta: 27 verbose_name = '分类' 28 verbose_name_plural = verbose_name 29 #按照index和id属性排序 30 ordering = ['index', 'id'] 31 32 def __str__(self): 33 return self.name 34 35 # 用户模型 36 # 继承方式扩展AbstractUser,可以使用django封装好的权限等属性 37 # 扩展:还可以用关联方式扩展用户信息,one to one 38 # 两者各有优劣 39 class User(AbstractUser): 40 #头像用图片类型 41 avatar = models.ImageField(\ 42 upload_to='avatar/%Y/%m', default='avatar/default.png', \ 43 max_length=200, blank=True, null=True, verbose_name='用户头像') 44 #可以为空 45 qq = models.CharField(max_length=20, blank=True, null=True, verbose_name='QQ号码') 46 #不能重复 47 mobile = models.CharField(max_length=11, \ 48 blank=True, null=True, unique=True, verbose_name='手机号码') 49 #新增字段 50 url=models.URLField(max_length=100,blank=True,null=True,verbose_name='个人网页地址') 51 52 class Meta: 53 verbose_name = '用户' 54 verbose_name_plural = verbose_name 55 ordering = ['-id'] 56 57 def __str__(self): 58 return self.username 59 # 自定义文章管理器 60 # 1.新加一个数据处理的方法(这里采用) 61 # 2.改变原有的Queryse方法 62 class ArticleManager(models.Manager): 63 def distinct_date(self): 64 distinct_date_list = [] 65 #取出日期字段 66 date_list = self.values('date_publish') 67 for date in date_list: 68 #取出的日期先转换格式 69 date = date['date_publish'].strftime('%Y/%m文档存档') 70 if date not in distinct_date_list: 71 distinct_date_list.append(date) 72 return distinct_date_list 73 # 文章模型 74 class Article(models.Model): 75 title = models.CharField(max_length=50, verbose_name='文章标题') 76 desc = models.CharField(max_length=50, verbose_name='文章描述') 77 content = models.TextField(verbose_name='文章内容') 78 click_count = models.IntegerField(default=0, verbose_name='点击次数') 79 is_recommend = models.BooleanField(default=False, verbose_name='是否推荐') 80 date_publish = models.DateTimeField(auto_now_add=True, verbose_name='发布时间') 81 user = models.ForeignKey(User, verbose_name='用户') 82 #外键 83 category = models.ForeignKey(Category, blank=True, null=True, verbose_name='分类') 84 #多对多关系将会自动多生成一张表,blog_article_tag 85 tag = models.ManyToManyField(Tag, verbose_name='标签') 86 #在文章模型中加入自定义的管理器 87 objects = ArticleManager() 88 89 class Meta: 90 verbose_name = '文章' 91 verbose_name_plural = verbose_name 92 ordering = ['-date_publish'] 93 94 def __str__(self): 95 return self.title 96 # 自定义评论管理器 97 # 为了写入原生sql,返回按article分组统计排序的方法 98 class CommentManager(models.Manager): 99 def with_counts(self): 100 from django.db import connection 101 cursor = connection.cursor() 102 cursor.execute(""" 103 select article_id from blog_comment 104 group by article_id 105 order by count(article_id) 106 desc 107 """) 108 result_list = [] 109 #pdb.set_trace() 110 #取出来的是元组类型 111 for row in cursor.fetchall(): 112 id = row[0] 113 result_list.append(id) 114 return result_list 115 # 评论模型 116 class Comment(models.Model): 117 content = models.TextField(verbose_name='评论内容') 118 username = models.CharField(max_length =\ 119 30,blank=True,null=True,verbose_name='用户名') 120 email = models.EmailField(max_length=50,blank=True,null=True,verbose_name='邮箱地址') 121 url =\ 122 models.URLField(max_length=100,blank=True,null=True,verbose_name='个人网页地址') 123 date_publish = models.DateTimeField(auto_now_add=True, verbose_name='发布时间') 124 user = models.ForeignKey(User, blank=True, null=True, verbose_name='用户') 125 article = models.ForeignKey(Article,related_name="entries", blank=True, null=True, verbose_name='文章') 126 pid = models.ForeignKey('self', blank=True, null=True, verbose_name='父级评论') 127 # 使用自定义的管理器 128 objects = CommentManager() 129 130 class Meta: 131 verbose_name = '评论' 132 verbose_name_plural = verbose_name 133 #按发布日期倒序排列 134 ordering = ['-date_publish'] 135 136 def __str__(self): 137 return str(self.content) 138 139 # 友情链接 140 class Links(models.Model): 141 title = models.CharField(max_length=50, verbose_name='标题') 142 description = models.CharField(max_length=200, verbose_name='友情链接描述') 143 callback_url = models.URLField(verbose_name='url地址') 144 date_publish = models.DateTimeField(auto_now_add=True, verbose_name='发布时间') 145 index = models.IntegerField(default=999, verbose_name='排列顺序(从小到大)') 146 147 class Meta: 148 verbose_name = '友情链接' 149 verbose_name_plural = verbose_name 150 ordering = ['index', 'id'] 151 152 def __str__(self): 153 return self.title 154 155 # 广告 156 class Ad(models.Model): 157 title = models.CharField(max_length=50, verbose_name='广告标题') 158 description = models.CharField(max_length=200, verbose_name='广告描述') 159 image_url = models.ImageField(upload_to='ad/%Y/%m', verbose_name='图片路径') 160 callback_url = models.URLField(null=True, blank=True, verbose_name='回调url') 161 date_publish = models.DateTimeField(auto_now_add=True, verbose_name='发布时间') 162 index = models.IntegerField(default=999, verbose_name='排列顺序(从小到大)') 163 164 class Meta: 165 verbose_name = u'广告' 166 verbose_name_plural = verbose_name 167 ordering = ['index', 'id'] 168 169 def __str__(self): 170 return self.title
View Code
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:django 之数据库模块 - Python技术站