<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    海上月明

    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]

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


    網站導航:
     
    主站蜘蛛池模板: 四虎成人精品永久免费AV| 精品亚洲成a人在线观看| 亚洲最大av无码网址| 亚洲综合av一区二区三区| 成人免费午夜无码视频| 亚洲一区二区三区高清视频| h在线观看视频免费网站| 亚洲网址在线观看| 欧洲乱码伦视频免费国产| 免费a级毛片永久免费| 亚洲精品资源在线| 全免费毛片在线播放| 亚洲精品一卡2卡3卡四卡乱码| 影音先锋在线免费观看| 色噜噜噜噜亚洲第一| 亚洲国产精品无码久久九九| 亚洲免费无码在线| 成在线人永久免费视频播放| 亚洲AV色欲色欲WWW| 亚洲色精品vr一区二区三区| 无码人妻一区二区三区免费n鬼沢 无码人妻一区二区三区免费看 | 亚洲精品tv久久久久久久久久| 欧洲精品码一区二区三区免费看| 77777亚洲午夜久久多人| 久久爰www免费人成| 亚洲日本国产综合高清| 国产又粗又长又硬免费视频 | 两个人看的www免费视频| 亚洲国产美国国产综合一区二区 | 亚洲一区二区三区夜色| 一级毛片在线完整免费观看| 亚洲日本va中文字幕久久| 在线日本高清免费不卡| 亚洲精品无码mⅴ在线观看| 亚洲黄片毛片在线观看| 性xxxx视频免费播放直播| 亚洲日本一线产区和二线| 国产亚洲人成无码网在线观看| 鲁啊鲁在线视频免费播放| 区久久AAA片69亚洲| 亚洲第一成年免费网站|