Opened 4 years ago

Closed 4 years ago

Last modified 4 years ago

#24613 closed Cleanup/optimization (fixed)

Documentation Note for defer might need an example.

Reported by: Richard Eames Owned by: nobody
Component: Documentation Version: 1.8
Severity: Normal Keywords:
Cc: andrei.avk@… Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

The paragraph in the note section of the defer documentation is not immediately obvious to what it's implying. My understanding is that it's suggesting that one can use two django models to load data from a single database table, is that the only implication, or is there another?

I think adding an example would help to clarify what exactly is meant by the paragraph.

This is what I think it means:

class MyCommonlyUsedModel(models.Model):
    class Meta:
        managed = False
        db_table = 'app_largetable'
    f1 = models.Field(...)
    f2 = models.Field(...)

class MyUsuallyDeferredFieldsModel(models.Model):
    class Meta:
        managed = False
        db_table = 'app_largetable'
    deferred_f3 = models.Field(...)
    deferred_f4 = models.Field(...)

# A model that allows the migrations framework to also manage the table
# Not sure if this actually works, but would allow for a more DRY approach
class MyManagedModel(MyCommonlyUsedModel, MyUsuallyDeferredFieldsModel):
    class Meta:
        managed = True
        tb_table = 'app_largetable'

If the above example is acceptable as an example - and is actually what the note is about - I don't mind creating a PR.

Change History (10)

comment:1 Changed 4 years ago by Tim Graham

Triage Stage: UnreviewedAccepted

Yes, an example wouldn't hurt. There wasn't much additional context in the commit where that text was added 70e59aeaf85161ed26044c000a43af46719265ad.

Did you test your example? I am not sure field inheritance will work properly with suggested approach, but maybe so. To make the example simpler, there probably isn't a need for MyUsuallyDeferredFieldsModel (just put those fields on MyManagedModel).

comment:2 Changed 4 years ago by andrei kulakov

In this paragraph:

only use defer() when you cannot, at queryset load time, determine if you will need the extra fields or not.

Shouldn't it be, instead, ".. when you cannot, before the queryset load time, determine if you will need the extra fields or not." ?

comment:3 Changed 4 years ago by Tim Graham

Maybe, actually I am not quite sure what the intention of the wording is there.

comment:4 Changed 4 years ago by andrei kulakov

Cc: andrei.avk@… added

I haven't used defer() myself before, but from reading the section, the idea seems to be that: if you can determine what fields need to be left out before queryset load time, use some other solution; if you can NOT do that, that's the only case where you'd use defer() because that's your only option.

comment:5 Changed 4 years ago by Tim Graham

What does "queryset load time" mean? When the queryset is evaluated and the database query occurs, or something else?

comment:6 Changed 4 years ago by Richard Eames

Has patch: set

Created a pull request with a minimal example.

https://github.com/django/django/pull/4539

comment:7 in reply to:  5 Changed 4 years ago by andrei kulakov

Replying to timgraham:

What does "queryset load time" mean? When the queryset is evaluated and the database query occurs, or something else?

I think it means when .only() or .defer() method is executed; -- there's two time points of interest: module load time, when you can define a module with a subset of fields, and queryset object creation time (or modification of existing queryset), when you provide the list of fields to defer. These are the only two times when you could provide this list of fields, so if you know the list at the module load time, it's preferred to define it as a model with a subset of fields, but if you don't know at module load time, then you don't have this option and you only have defer() and only() left to use.

Last edited 4 years ago by andrei kulakov (previous) (diff)

comment:8 Changed 4 years ago by Tim Graham <timograham@…>

Resolution: fixed
Status: newclosed

In dd99f577:

Fixed #24613 -- Added example to QuerySet.defer() documentation

comment:9 Changed 4 years ago by Tim Graham <timograham@…>

In bcd7f39:

[1.7.x] Fixed #24613 -- Added example to QuerySet.defer() documentation

Backport of dd99f57710bb4930561a6c049f54719af80850ec from master

comment:10 Changed 4 years ago by Tim Graham <timograham@…>

In fe533fc5:

[1.8.x] Fixed #24613 -- Added example to QuerySet.defer() documentation

Backport of dd99f57710bb4930561a6c049f54719af80850ec from master

Note: See TracTickets for help on using tickets.
Back to Top