海上月明

          editer by sun
          posts - 162, comments - 51, trackbacks - 0, articles - 8
             :: 首頁 :: 新隨筆 ::  :: 聚合  :: 管理

          Django的幾個Database API Note

          Posted on 2006-12-11 20:23 pts 閱讀(670) 評論(0)  編輯  收藏
          __exact        精確等于 like 'aaa'
          __iexact    精確等于 忽略大小寫 ilike 'aaa'
          __contains    包含 like '%aaa%'
          __icontains    包含 忽略大小寫 ilike '%aaa%',但是對于sqlite來說,contains的作用效果等同于icontains。
          __gt     大于
          __gte    大于等于
          __lt      小于
          __lte     小于等于
          __in     存在于一個list范圍內
          __startswith   以...開頭
          __istartswith   以...開頭 忽略大小寫
          __endswith     以...結尾
          __iendswith    以...結尾,忽略大小寫
          __range    在...范圍內
          __year       日期字段的年份
          __month    日期字段的月份
          __day        日期字段的日
          __isnull=True/False

          __isnull=True 與 __exact=None的區別
          There
          is an important difference between __isnull=True and __exact=None.
          __exact=None will always return an empty result set, because SQL
          requires that no value is equal to NULL. __isnull determines if the
          field is currently holding the value of NULL without performing a
          comparison.

          默認的查找是精確查找

          下面的三個查詢結果相同
          Blog.objects.get(id__exact=14) # Explicit form
          Blog.objects.get(id=14)              # __exact is implied
          Blog.objects.get(pk=14)             # pk implies id__exact

          查詢集的比較:
          下面的兩個比較語句效果相同:
          some_entry == other_entry
          some_entry.id == other_entry.id
          如果一個對象的主鍵名不叫id,沒關系,some_entry == other_entry的比較會自動查找各自主鍵進行比較。如果對象的主鍵名為name,那么下面的語句效果相同:
          some_obj == other_obj
          some_obj.name == other_obj.name

          Q表達式如果和其他條件語句一同使用,必須放在前面。


          Forward access to one-to-many relationships is cached the first time the
          related object is accessed. Subsequent accesses to the foreign key on the same
          object instance are cached. Example:

          e = Entry.objects.get(id=2)
          print e.blog # Hits the database to retrieve the associated Blog.
          print e.blog # Doesn't hit the database; uses cached version.

          Note that the select_related() QuerySet method recursively prepopulates
          the cache of all one-to-many relationships ahead of time. Example:

          e = Entry.objects.select_related().get(id=2)
          print e.blog # Doesn't hit the database; uses cached version.
          print e.blog # Doesn't hit the database; uses cached version.



          select_related() is documented in the "QuerySet methods that return new
          QuerySets" section above.

          可以在Blog對象中使用entry_set引用entry對象:
          b = Blog.objects.get(id=1)
          b.entry_set.all() # Returns all Entry objects related to Blog.

          #entry_set返回的結果集可以執行過濾器功能:
          b.entry_set.filter(headline__contains='Lennon')
          b.entry_set.count()

          對于entry_set的名稱可以通過在定義模型的外鍵時自定義:
          blog = ForeignKey(Blog, related_name='entries')
          此后就可以在引用外鍵所在隊形時使用如下方式:
          b = Blog.objects.get(id=1)
          b.entries.all() # Returns all Entry objects related to Blog.
          You cannot access a reverse ForeignKey Manager from the class; it must
          be accessed from an instance. Example:

          Blog.entry_set # Raises AttributeError: "Manager must be accessed via instance".

          只能是:

          b.entry_set



          實體引用集的方法:

          add:

          b = Blog.objects.get(id=1)

          e = Entry.objects.get(id=234)

          b.entry_set.add(e) # Associates Entry e with Blog b.

          create:

          b = Blog.objects.get(id=1)

          e = b.entry_set.create(headline='Hello', body_text='Hi', pub_date=datetime.date(2005, 1, 1))#不再需要save()方法。



          remove:

          this method only exists on
          ForeignKey objects where null=True.

          b = Blog.objects.get(id=1)

          e = Entry.objects.get(id=234)

          b.entry_set.remove(e) # Disassociates Entry e from Blog b.



          clear:

          同remove的限制條件

          b = Blog.objects.get(id=1)

          b.entry_set.clear()



          查詢條件中使用實體的幾個示例:

          if you have a Blog object b with id=5, the following
          three queries would be identical:

          Entry.objects.filter(blog=b) # Query using object instance

          Entry.objects.filter(blog=b.id) # Query using id from instance

          Entry.objects.filter(blog=5) # Query using id directly



          delete方法:

          是一個查詢結果集的方法,而不是實體模型的方法,如:

          Entry.objects.filter(pub_date__year=2005).delete()     #正確

          Entry.objects.all().delete()       #不正確




          get_FOO_display():


          For every field that has choices set, the object will have a
          get_FOO_display() method, where FOO is the name of the field. This
          method returns the "human-readable" value of the field. For example, in the
          following model:


          GENDER_CHOICES = (
          ('M', 'Male'),
          ('F', 'Female'),
          )
          class Person(models.Model):
          name = models.CharField(maxlength=20)
          gender = models.CharField(maxlength=1, choices=GENDER_CHOICES)

          ...each Person instance will have a get_gender_display() method. Example:


          >>> p = Person(name='John', gender='M')
          />>> p.save()
          />>> p.gender
          'M'
          />>> p.get_gender_display()
          'Male'


          取得最大最小id記錄

          MIN == Entry.objects.order_by()[0]

          MAX == Entry.objects.order_by('-id'][0]

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 满洲里市| 武宁县| 永新县| 兴宁市| 和田县| 凤凰县| 邯郸市| 开化县| 迭部县| 建阳市| 南澳县| 拉孜县| 阿图什市| 长乐市| 德格县| 延川县| 镇赉县| 宁武县| 中牟县| 梁山县| 泉州市| 邵武市| 绍兴市| 溧阳市| 缙云县| 轮台县| 宁强县| 濮阳县| 松阳县| 绥德县| 颍上县| 简阳市| 城步| 涡阳县| 若尔盖县| 新巴尔虎右旗| 武强县| 郧西县| 水富县| 延川县| 裕民县|