Opened 9 years ago

Closed 9 years ago

Last modified 9 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 by Tim Graham, 9 years ago

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 by andrei kulakov, 9 years ago

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 by Tim Graham, 9 years ago

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

comment:4 by andrei kulakov, 9 years ago

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 by Tim Graham, 9 years ago

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

comment:6 by Richard Eames, 9 years ago

Has patch: set

Created a pull request with a minimal example.

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

in reply to:  5 comment:7 by andrei kulakov, 9 years ago

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 9 years ago by andrei kulakov (previous) (diff)

comment:8 by Tim Graham <timograham@…>, 9 years ago

Resolution: fixed
Status: newclosed

In dd99f577:

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

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

In bcd7f39:

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

Backport of dd99f57710bb4930561a6c049f54719af80850ec from master

comment:10 by Tim Graham <timograham@…>, 9 years ago

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