美文网首页
extra & defer & only

extra & defer & only

作者: whenitsallover | 来源:发表于2018-03-19 00:43 被阅读0次

    defer()

    official link
    https://docs.djangoproject.com/en/2.0/ref/models/querysets/#defer

    In some complex data-modeling situations, your models might contain a lot of fields, some of which could contain a lot of data (for example, text fields), or require expensive processing to convert them to Python objects. If you are using the results of a queryset in some situation where you don’t know if you need those particular fields when you initially fetch the data, you can tell Django not to retrieve them from the database.

    This is done by passing the names of the fields to not load to defer():

    Entry.objects.defer("headline", "body")
    

    A queryset that has deferred fields will still return model instances. Each deferred field will be retrieved from the database if you access that field (one at a time, not all the deferred fields at once).

    You can make multiple calls to defer(). Each call adds new fields to the deferred set:

    # Defers both the body and headline fields.
    Entry.objects.defer("body").filter(rating=5).defer("headline")
    

    only()

    official link
    https://docs.djangoproject.com/en/2.0/ref/models/querysets/#only
    The only() method is more or less the opposite of defer(). You call it with the fields that should not be deferred when retrieving a model. If you have a model where almost all the fields need to be deferred, using only() to specify the complementary set of fields can result in simpler code.

    Suppose you have a model with fields name, age and biography. The following two querysets are the same, in terms of deferred fields:

    Person.objects.defer("age", "biography")
    Person.objects.only("name")
    

    Whenever you call only() it replaces the set of fields to load immediately. The method’s name is mnemonic: only those fields are loaded immediately; the remainder are deferred. Thus, successive calls to only() result in only the final fields being considered:

    # This will defer all fields except the headline.
    Entry.objects.only("body", "rating").only("headline")
    

    Since defer() acts incrementally (adding fields to the deferred list), you can combine calls to only() and defer() and things will behave logically:

    # Final result is that everything except "headline" is deferred.
    Entry.objects.only("headline", "body").defer("body")
    
    # Final result loads headline and body immediately (only() replaces any
    # existing set of fields).
    Entry.objects.defer("body").only("headline", "body")
    

    All of the cautions in the note for the defer() documentation apply to only() as well. Use it cautiously and only after exhausting your other options.

    Using only() and omitting a field requested using select_related() is an error as well.

    extra()

    About to be deprecated.

    相关文章

      网友评论

          本文标题:extra & defer & only

          本文链接:https://www.haomeiwen.com/subject/hbxuqftx.html