Monday, September 12, 2011

Re: Alter the list of fields returned by a queryset without turning them into strings?

On Mon, Sep 12, 2011 at 8:41 PM, Joshua Russo <josh.r.russo@gmail.com> wrote:
> Do I just need to do a little more pre-processing in the view or can I
> accomplish what I'm trying to do?

The method you're looking for is ``only()`` (or perhaps its cousin,
``defer``): https://docs.djangoproject.com/en/dev/ref/models/querysets/#django.db.models.query.QuerySet.only.

``only()`` takes a list of fields like ``values()``, and constrains
the SELECT clause similarly, but instead of returning a dictionary
returns a model instance, so things like the FileFile objects work
correctly.

Do note that if you use ``defer()``/``only()`` and the access a field
*not* in the select list Django will go perform another query for you,
thus making things less efficient. So only reach for ``only()`` when
you know you're only accessing the specific named fields (as it looks
like you are in your example).

Good luck,

Jacob

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate