{{{
class MyModel(models.Model):
enormous_field = models.TextField(defer=True)
other_field = models.IntegerField()
}}}
Which is (simplistically) equivalent to the following:
{{{
class MyManager(models.Manager):
use_for_related_fields = True
def get_queryset(self, *args, **kwargs):
return super(MyManager, self).get_queryset(*args,
**kwargs).defer("enormous_field")
class MyModel(models.Model):
enormous_field = models.TextField()
other_field = models.IntegerField()
objects = MyManager()
}}}
Obviously, you can still do `MyModel.objects.defer(None)` to do the full
query.
--
Ticket URL: <https://code.djangoproject.com/ticket/23816>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* needs_better_patch: => 0
* needs_tests: => 0
* needs_docs: => 0
Comment:
I find this really useful, as most of the times you (myself actually,
assuming most people too) really don't make a manager just for this
optimisation. So defining deferred fields at model level makes sense for
me. Though I would prefer to specify the fields in the model's Meta
options, as a list of field names, similar to unique_together option.
--
Ticket URL: <https://code.djangoproject.com/ticket/23816#comment:1>
* cc: akiskesoglou@… (added)
--
Ticket URL: <https://code.djangoproject.com/ticket/23816#comment:2>
* stage: Unreviewed => Accepted
Comment:
I am a little hesitant to accept since there is already a way to
accomplish this, but tentatively accepting so we can see what a patch
looks like and someone else really like the idea.
--
Ticket URL: <https://code.djangoproject.com/ticket/23816#comment:3>
Comment (by carljm):
I'm also hesitant about adding something that's already doable via a
custom manager; I'm probably -0.
If we do it, it should definitely be a `Meta` option, not a field arg,
IMO. It applies to fields, but it's really a characteristic of querying on
the model class, not inherently of the Field.
--
Ticket URL: <https://code.djangoproject.com/ticket/23816#comment:4>
Comment (by dfunckt):
I'm inclined to have a go at this. Here's how I envision it:
{{{
class Post(models.Model):
body_text = models.TextField()
body_html = models.TextField(blank=True)
class Meta:
initially_deferred_fields = ('body_text', 'body_html')
}}}
Some thoughts:
- Not sure about the name of the meta attribute, it requires almost as
much effort to get the spelling right as creating a custom manager, but
I'd like to avoid naming it `defer` or `deferred_fields`. I'd like input
on this, but it's not pressing.
- The patch author should be aware of the model meta refactor, perhaps
beginning work after it lands?
--
Ticket URL: <https://code.djangoproject.com/ticket/23816#comment:5>
Comment (by willstott101):
I can see a use for this. Currently using a manager which looks at
`auto_defer` (on the manager due to
[https://code.djangoproject.com/ticket/5793 5793]).
{{{
class AutoDeferManagerMixin(object):
use_for_related_fields = True
auto_defer = tuple()
def get_queryset(self, *args, **kwargs):
return super(AutoDeferManagerMixin, self).get_queryset(*args,
**kwargs).defer(*self.auto_defer)
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/23816#comment:6>
* status: new => assigned
* owner: nobody => Denis.Tarykin
--
Ticket URL: <https://code.djangoproject.com/ticket/23816#comment:7>
* has_patch: 0 => 1
Comment:
PR - https://github.com/django/django/pull/9309
--
Ticket URL: <https://code.djangoproject.com/ticket/23816#comment:8>
* needs_better_patch: 0 => 1
Comment:
Marking "Patch needs improvement" pending comments on PR
--
Ticket URL: <https://code.djangoproject.com/ticket/23816#comment:9>
Comment (by Mariusz Felisiak):
#24096 was a duplicate.
--
Ticket URL: <https://code.djangoproject.com/ticket/23816#comment:10>
* cc: Carlos Palol (added)
--
Ticket URL: <https://code.djangoproject.com/ticket/23816#comment:11>
* status: assigned => closed
* resolution: => wontfix
* stage: Accepted => Unreviewed
Comment:
This ticket was tentatively accepted and we have several opinions against
its implementation ([https://code.djangoproject.com/ticket/23816#comment:4
Carl],
[https://github.com/django/django/pull/14110#pullrequestreview-610972680
Simon],
[https://github.com/django/django/pull/14110#issuecomment-830777943 Adam],
we can add me to this list).
Marking as "wontfix", because it seems we need to reach a consensus on the
DevelopersMailingList before moving forward. Please
[https://docs.djangoproject.com/en/stable/internals/contributing/triaging-
tickets/#closing-tickets follow the triaging guidelines with regards to
wontfix tickets] and take this to DevelopersMailingList to reach a wider
audience and see what other think.
--
Ticket URL: <https://code.djangoproject.com/ticket/23816#comment:12>