一、django进阶
1、django orm 增删改查
1.1、创建表:
1
2
3
|
>>> from blog.models import Blog
>>> b = Blog(name = 'Beatles Blog' , tagline = 'All the latest Beatles news.' )
>>> b.save() |
This performs an INSERT
SQL statement behind the scenes. Django doesn’t hit the database until you explicitly call save()
.
The save()
method has no return value.
处理带外键关联或多对多关联的对象
ForeignKey的关联
1
2
3
4
5
|
>>> from blog.models import Entry
>>> entry = Entry.objects.get(pk = 1 )
>>> cheese_blog = Blog.objects.get(name = "Cheddar Talk" )
>>> entry.blog = cheese_blog
>>> entry.save() |
ManyToManyField关联
1
2
3
|
>>> from blog.models import Author
>>> joe = Author.objects.create(name = "Joe" )
>>> entry.authors.add(joe) |
添加多个ManyToMany对象
1
2
3
4
5
|
>>> john = Author.objects.create(name = "John" )
>>> paul = Author.objects.create(name = "Paul" )
>>> george = Author.objects.create(name = "George" )
>>> ringo = Author.objects.create(name = "Ringo" )
>>> entry.authors.add(john, paul, george, ringo) |
1.2、查询
单表查询:
all_entries = Entry.objects.all() #查询所有
Entry.objects.filter(pub_date__year=2006) #查询所有pub_date为2006年的纪录
Entry.objects.all().filter(pub_date__year=2006) #与上面那句一样
>>> Entry.objects.filter( #链式查询
... headline__startswith='What'
... ).exclude(
... pub_date__gte=datetime.date.today()
... ).filter(
... pub_date__gte=datetime(2005, 1, 30)
... ) one_entry = Entry.objects.get(pk=1) #单条查询 Entry.objects.all()[:5] #查询前5条
Entry.objects.all()[5:10] #你猜 Entry.objects.order_by('headline')[0] #按headline排序取第一条 Entry.objects.filter(pub_date__lte='2006-01-01') #相当于sql语句SELECT * FROM blog_entry WHERE pub_date <= '2006-01-01'; Entry.objects.get(headline__exact="Cat bites dog") #相当于SELECT ... WHERE headline = 'Cat bites dog';
Blog.objects.get(name__iexact="beatles blog") #与上面相同,只是大小写不敏感 Entry.objects.get(headline__contains='Lennon') #相当 于SELECT ... WHERE headline LIKE '%Lennon%';
关联查询:
#This example retrieves all Entry objects with a Blog whose name is 'Beatles Blog':
Entry.objects.filter(blog__name='Beatles Blog') Blog.objects.filter(entry__headline__contains='Lennon')
对同一表内不同的字段进行对比查询,In the examples given so far, we have constructed filters that compare the value of a model field with a constant. But what if you want to compare the value of a model field with another field on the same model?
Django provides F expressions
to allow such comparisons. Instances of F()
act as a reference to a model field within a query. These references can then be used in query filters to compare the values of two different fields on the same model instance.
For example, to find a list of all blog entries that have had more comments than pingbacks, we construct an F()
object to reference the pingback count, and use that F()
object in the query:
1
2
|
>>> from django.db.models import F
>>> Entry.objects. filter (n_comments__gt = F( 'n_pingbacks' ))
|
Django supports the use of addition, subtraction, multiplication, division, modulo, and power arithmetic with F()
objects, both with constants and with other F()
objects. To find all the blog entries with more than twice as many comments as pingbacks, we modify the query:
1
|
>>> Entry.objects. filter (n_comments__gt = F( 'n_pingbacks' ) * 2 )
|
To find all the entries where the rating of the entry is less than the sum of the pingback count and comment count, we would issue the query:
1
|
>>> Entry.objects. filter (rating__lt = F( 'n_comments' ) + F( 'n_pingbacks' ))
|
For date and date/time fields, you can add or subtract a timedelta
object. The following would return all entries that were modified more than 3 days after they were published:
1
2
|
>>> from datetime import timedelta
>>> Entry.objects. filter (mod_date__gt = F( 'pub_date' ) + timedelta(days = 3 ))
|
更多复杂装逼的查询可以查看django文档。
1.3、更新
Updating multiple objects at once
1
2
|
# Update all the headlines with pub_date in 2007. Entry.objects. filter (pub_date__year = 2007 ).update(headline = 'Everything is the same' )
|
在原有数据的基础上批量自增
Calls to update can also use F expressions
to update one field based on the value of another field in the model. This is especially useful for incrementing counters based upon their current value. For example, to increment the pingback count for every entry in the blog:
1
|
>>> Entry.objects. all ().update(n_pingbacks = F( 'n_pingbacks' ) + 1 )
|
However, unlike F()
objects in filter and exclude clauses, you can’t introduce joins when you use F()
objects in an update – you can only reference fields local to the model being updated. If you attempt to introduce a join with an F()
object, a FieldError
will be raised:
1
2
|
# THIS WILL RAISE A FieldError >>> Entry.objects.update(headline = F( 'blog__name' ))
|
1.4、聚合
https://docs.djangoproject.com/en/1.9/topics/db/aggregation/
2、django form
django中的Form一般有两种功能:
- 输入html
- 验证用户输入
表单验证
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import re
from django import forms
from django.core.exceptions import ValidationError def mobile_validate(value):
mobile_re = re.compile(r'^(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$')
if not mobile_re.match(value):
raise ValidationError('手机号码格式错误') class PublishForm(forms.Form): user_type_choice = (
(0, u'普通用户'),
(1, u'高级用户'),
) user_type = forms.IntegerField(widget=forms.widgets.Select(choices=user_type_choice,
attrs={'class': "form-control"})) title = forms.CharField(max_length=20,
min_length=5,
error_messages={'required': u'标题不能为空',
'min_length': u'标题最少为5个字符',
'max_length': u'标题最多为20个字符'},
widget=forms.TextInput(attrs={'class': "form-control",
'placeholder': u'标题5-20个字符'})) memo = forms.CharField(required=False,
max_length=256,
widget=forms.widgets.Textarea(attrs={'class': "form-control no-radius", 'placeholder': u'详细描述', 'rows': 3})) phone = forms.CharField(validators=[mobile_validate, ],
error_messages={'required': u'手机不能为空'},
widget=forms.TextInput(attrs={'class': "form-control",
'placeholder': u'手机号码'})) email = forms.EmailField(required=False,
error_messages={'required': u'邮箱不能为空','invalid': u'邮箱格式错误'},
widget=forms.TextInput(attrs={'class': "form-control", 'placeholder': u'邮箱'}))
form
def publish(request):
ret = {'status': False, 'data': '', 'error': '', 'summary': ''}
if request.method == 'POST':
request_form = PublishForm(request.POST)
if request_form.is_valid():
request_dict = request_form.clean()
print request_dict
ret['status'] = True
else:
error_msg = request_form.errors.as_json()
ret['error'] = json.loads(error_msg)
return HttpResponse(json.dumps(ret))
View
modelform验证
在使用Model和Form时,都需要对字段进行定义并指定类型,通过ModelForm则可以省去From中字段的定义
class AdminModelForm(forms.ModelForm): class Meta:
model = models.Admin
#fields = '__all__'
fields = ('username', 'email') widgets = {
'email' : forms.PasswordInput(attrs={'class':"alex"}),
}
3、django类视图
4、django admin配置
django amdin是django提供的一个后台管理页面,改管理页面提供完善的html和css,使得你在通过Model创建完数据库表之后,就可以对数据进行增删改查,而使用django admin 则需要以下步骤:
- 创建后台管理员
- 配置url
- 注册和配置django admin后台管理页面
1、创建后台管理员
1
|
python manage.py createsuperuser |
2、配置后台管理url
1
|
url(r '^admin/' , include(admin.site.urls))
|
3、注册和配置django admin 后台管理页面
a、在admin中执行如下配置
1
2
3
4
5
6
7
8
|
from django.contrib import admin
from app01 import models
admin.site.register(models.UserType) admin.site.register(models.UserInfo) admin.site.register(models.UserGroup) admin.site.register(models.Asset) |
b、设置数据表名称
1
2
3
4
5
6
|
class UserType(models.Model):
name = models.CharField(max_length = 50 )
class Meta:
verbose_name = '用户类型'
verbose_name_plural = '用户类型'
|
c、打开表之后,设定默认显示,需要在model中作如下配置
1
2
3
4
5
|
class UserType(models.Model):
name = models.CharField(max_length = 50 )
def __unicode__( self ):
return self .name
|
1
2
3
4
5
6
7
8
9
10
11
12
|
from django.contrib import admin
from app01 import models
class UserInfoAdmin(admin.ModelAdmin):
list_display = ( 'username' , 'password' , 'email' )
admin.site.register(models.UserType) admin.site.register(models.UserInfo,UserInfoAdmin) admin.site.register(models.UserGroup) admin.site.register(models.Asset) |
d、为数据表添加搜索功能
1
2
3
4
5
6
7
8
9
10
11
12
|
from django.contrib import admin
from app01 import models
class UserInfoAdmin(admin.ModelAdmin):
list_display = ( 'username' , 'password' , 'email' )
search_fields = ( 'username' , 'email' )
admin.site.register(models.UserType) admin.site.register(models.UserInfo,UserInfoAdmin) admin.site.register(models.UserGroup) admin.site.register(models.Asset) |
e、添加快速过滤
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
from django.contrib import admin
from app01 import models
class UserInfoAdmin(admin.ModelAdmin):
list_display = ( 'username' , 'password' , 'email' )
search_fields = ( 'username' , 'email' )
list_filter = ( 'username' , 'email' )
admin.site.register(models.UserType) admin.site.register(models.UserInfo,UserInfoAdmin) admin.site.register(models.UserGroup) admin.site.register(models.Asset) |
二、BBS项目
1、需求分析
- 整体参考“抽屉新热榜” + “虎嗅网”
- 实现不同论坛版块
- 帖子列表展示
- 帖子评论数、点赞数展示
- 允许登录用户发贴、评论、点赞
- 允许上传文件
- 帖子可被置顶
- 可进行多级评论
知识点:
- Django
- HTML\CSS\JS
- BootStrap
- Jquery
2、用户场景分析
游客(非登陆状态):可浏览所有文章
登陆状态:可浏览、可评论、可点赞
3、表结构设计
from django.db import models
from django.contrib.auth.models import User # Create your models here. class UserProifle(models.Model):
user = models.OneToOneField(User,null=True,default=None)
name = models.CharField(max_length=32) def __str__(self):
return self.name class Article(models.Model):
"""文章表"""
title = models.CharField(max_length=128,unique=True)
author = models.ForeignKey("UserProifle")
category = models.ForeignKey("Category")
pub_date = models.DateTimeField(auto_now_add=True,auto_created=True)
tags = models.ManyToManyField("Tag", null=True)
body = models.TextField(max_length=100000)
head_img = models.ImageField(upload_to="uploads")
status_choices = ((0,'草稿'),(1,'发布'),(2,'隐藏'))
priority = models.SmallIntegerField(default=1000,verbose_name="优先级") def __str__(self):
return self.title class Category(models.Model):
"""板块"""
name = models.CharField(max_length=64,unique=True)
set_as_top_menu = models.BooleanField(default=True) def __str__(self):
return self.name class Tag(models.Model):
"""标签表"""
name = models.CharField(max_length=64, unique=True)
def __str__(self):
return self.name class Comment(models.Model):
"""评论"""
article = models.ForeignKey("Article")
p_node = models.ForeignKey("Comment",null=True,blank=True,related_name="my_child_comments") user = models.ForeignKey("UserProifle")
date = models.DateTimeField(auto_now_add=True)
comment = models.TextField(max_length=1024) def __str__(self):
return self.comment class Like(models.Model):
"""点赞"""
article = models.ForeignKey("Article")
user = models.ForeignKey("UserProifle")
date = models.DateTimeField(auto_now_add=True) class PrivateMail(models.Model):
"""私信"""
pass
表结构设计