## ORM operation

    General Operation

      It will be part:

  all ()  to query all the results, the final result is QuerySet object.

models.Press.objects.all()  # 查询所有出版社

  ** get (** kwargs) ** Returns the object and screening conditions to match the returns only one result, if the objects that meet the filter criteria of more than one or none, will throw an error. The end result is an object.

models.Press.objects.get(id=1)  # 查询id等于1的出版社

  ** filter (** kwargs) ** that contains all objects that match the filter to the final result is returned QuerySet object.

models.Press.objects.filter(id=1)  # 查询id等于1的所有出版社

  ** exclude (** kwargs) ** all the objects it contains and the filters do not match a given final result is QuerySet object.

models.Press.objects.exclude(id=1)  # 查询id不等于1的所有出版社

  values (* field)  Returns a ValueQuerySet - a special QuerySet, obtained after running the model does not instantiate the object of a series. But a dictionary can be iterative sequence. Simply put, it is ValueQuerySet list contains the query to the results of each result is a dictionary, the dictionary is the key field field, is found in the results. When the time field does not write the query to all fields.

models.Press.objects.all().values()  # 查询所有对形象的所有字段,每条记录为一个字典

models.Press.objects.all().values('id', 'name')  # 查询所有对象的id和name字段,每条记录为一个字典

  values_list (* field)  and values () are very similar, the difference is that he is a return to the ancestral sequence.

models.Press.objects.all().values_list()  # 查询所有对形象的所有字段,每条记录为一个元祖,顺序为values_list()中填写的字段的顺序

models.Press.objects.all().values_list('id', 'name')  # 查询所有对象的id和name字段,每条记录为一个元祖,顺序为values_list()中填写的字段的顺序

  order_by (* field)  to sort the final results of the query result set QuerySet object to the * field when a minus sign in front. (-), the descending order.

1 models.Press.objects.all().order_by('-id')  # 查询结果按照id降序排列.结果为QuerySet对象.
2 
3 models.Press.objects.all().order_by('age','-id')  # 查询结果先按照age字段增序排列,出现相同age时再按照id降序排列.结果为QuerySet对象.

  reverse ()  to sort query results in reverse order, note the reverse () usually only have to call on a defined sort QuerySet (order_by specified in the Meta model class () or call ordering method.).

models.Person.objects.all().reverse()

  distinct ()  eliminate duplicate records (if you query across multiple tables, you may get duplicate results when calculating the QuerySet. At this point you can use distinct (), note that only supported by fields deduplication in PostgreSQL.) From the returned results, return result QuerySet object.

  count ()  returns the database match the query (QuerySet) the number of objects.

models.Person.objects.all().count()

  first ()  returns the first record (object) can be filled only when the first QuerySet that returns a value of ()

models.Person.objects.all().first()

  last ()  Returns the most one record (target), must be to use the last results are returned to QuerySet ()

models.Person.objects.all().last()

  exists ()  if QuerySet contains data, returns True, otherwise False.

models.Person.objects.all().exists()

    Single-table queries amazing value double underlined

  ** field names __gt ** greater than greater than

ret = models.Person.objects.filter(id__gt=1)  # 查询id大于1的所有对象

  ** field names __lt ** less than less than

ret = models.Person.objects.filter(id__lt=4)  # 查询id小于4的所有对象

  ** field name __gte ** greater than equal or greater

ret = models.Person.objects.filter(id__gte=1)  # 查询id大于等于1的所有对象

  ** field name __lte ** less than equal or less

ret = models.Person.objects.filter(id__lte=1)  # 查询id小于等于1的所有对象

  __In ** ** field names in what was

ret = models.Person.objects.filter(id__in=[1, 3])  # 查询id在列表里的所有对象

  ** field name __range = [Condition 1, Condition 2] ** equivalent to the field name __gte = 1 condition, the condition field name __lte = 2, where the comma (,) and represents the relationship. [Condition 1, Condition 2 ] as closed interval.

ret = models.Person.objects.filter(id__gte=1, id__lte=3)  # 查询id大于等于1,小于等于3的所有对象

ret = models.Person.objects.filter(id__range=[1, 3])  # 查询id在1到3之间的所有对象

  ** field name __contains = 'x' ** fuzzy find all objects that contain the x

ret = models.Person.objects.filter(name__contains='e')  # 查询name中含有e的所有对象

  ** field name __icontains = 'x' ** ignore case, fuzzy search of all objects containing x

ret = models.Person.objects.filter(name__icontains='e')  # 忽略大小,查找name中含有e的所有对象

  ** field name __startswith = 'o' ** match all the objects beginning with o

ret = models.Person.objects.filter(name__startswith='e')  # 匹配name中以e开头的所有对象

  ** field name __istartswith = 'o' ** ignore case, all the objects match the beginning o

ret = models.Person.objects.filter(name__istartswith='e')  # 忽略大小写,匹配name中以e开头的所有对象

  ** field name __endswith = 'x' ** match all of the objects to the end x

ret = models.Person.objects.filter(name__endswith='x')  # 匹配name中以x结尾的所有对象

  ** field name __iendswith = 'x' ** ignore case, all the objects to match the end x

ret = models.Person.objects.filter(name__iendswith='x')  # 忽略大小写,匹配name中以x结尾的所有对象
  date字段还可以:
models.Class.objects.filter(first_day__year=2017)

Guess you like

Origin www.cnblogs.com/zhuyuanying123--/p/11349629.html