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、查询

单表查询:

 1 all_entries = Entry.objects.all() #查询所有
 2 Entry.objects.filter(pub_date__year=2006) #查询所有pub_date为2006年的纪录
 3 Entry.objects.all().filter(pub_date__year=2006) #与上面那句一样
 4 >>> Entry.objects.filter(   #链式查询
 5 ...     headline__startswith='What'
 6 ... ).exclude(
 7 ...     pub_date__gte=datetime.date.today()
 8 ... ).filter(
 9 ...     pub_date__gte=datetime(2005, 1, 30)
10 ... )
11 
12 one_entry = Entry.objects.get(pk=1) #单条查询
13 
14 Entry.objects.all()[:5] #查询前5条
15 Entry.objects.all()[5:10] #你猜
16 
17 Entry.objects.order_by('headline')[0] #按headline排序取第一条
18 
19 Entry.objects.filter(pub_date__lte='2006-01-01') #相当于sql语句SELECT * FROM blog_entry WHERE pub_date <= '2006-01-01';
20 
21 Entry.objects.get(headline__exact="Cat bites dog") #相当于SELECT ... WHERE headline = 'Cat bites dog';
22 Blog.objects.get(name__iexact="beatles blog") #与上面相同,只是大小写不敏感
23 
24 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 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 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 FieldErrorwill 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
  • 验证用户输入

表单验证

 1 #!/usr/bin/env python
 2 # -*- coding:utf-8 -*-
 3 import re
 4 from django import forms
 5 from django.core.exceptions import ValidationError
 6 
 7 
 8 def mobile_validate(value):
 9     mobile_re = re.compile(r'^(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$')
10     if not mobile_re.match(value):
11         raise ValidationError('手机号码格式错误')
12 
13 
14 class PublishForm(forms.Form):
15 
16     user_type_choice = (
17         (0, u'普通用户'),
18         (1, u'高级用户'),
19     )
20 
21     user_type = forms.IntegerField(widget=forms.widgets.Select(choices=user_type_choice,
22                                                                   attrs={'class': "form-control"}))
23 
24     title = forms.CharField(max_length=20,
25                             min_length=5,
26                             error_messages={'required': u'标题不能为空',
27                                             'min_length': u'标题最少为5个字符',
28                                             'max_length': u'标题最多为20个字符'},
29                             widget=forms.TextInput(attrs={'class': "form-control",
30                                                           'placeholder': u'标题5-20个字符'}))
31 
32     memo = forms.CharField(required=False,
33                            max_length=256,
34                            widget=forms.widgets.Textarea(attrs={'class': "form-control no-radius", 'placeholder': u'详细描述', 'rows': 3}))
35 
36     phone = forms.CharField(validators=[mobile_validate, ],
37                             error_messages={'required': u'手机不能为空'},
38                             widget=forms.TextInput(attrs={'class': "form-control",
39                                                           'placeholder': u'手机号码'}))
40 
41     email = forms.EmailField(required=False,
42                             error_messages={'required': u'邮箱不能为空','invalid': u'邮箱格式错误'},
43                             widget=forms.TextInput(attrs={'class': "form-control", 'placeholder': u'邮箱'}))
form

相关文章:

  • 2022-12-23
  • 2021-09-14
  • 2022-12-23
  • 2022-01-05
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-01-21
  • 2022-12-23
  • 2021-06-07
  • 2021-08-07
  • 2022-12-23
  • 2021-06-12
  • 2021-11-04
相关资源
相似解决方案