Re: exclude fields from a query

1,123 views
Skip to first unread message

dj

unread,
Mar 26, 2009, 8:02:33 PM3/26/09
to Django users
Hello All,

I would like to know if there is a way to exclude fields from an
object query.

For example:

Suppose I have this model.

class Person(Model.model):
name = Charfield()
address = Charfield()
state = Charfield()
etc.... I do realize my syntax is probably wrong, please for
give me.

I want to create a query that will return the state attribute for each
object in the model.
I found a reference to a method called hide in the django
documentation.
It would worked as such:

p = Person.objects.hide('name', 'address', 'etc.').get('state')

However, when I attempted to use the method, I received an error
stating the method does not exist for the Manager. Does anyone know of
another way ?



dj

unread,
Mar 26, 2009, 8:05:58 PM3/26/09
to Django users
Here is a link to a django ticket which addressed the issue.

http://code.djangoproject.com/ticket/5420

DJ

Malcolm Tredinnick

unread,
Mar 26, 2009, 8:38:28 PM3/26/09
to django...@googlegroups.com
On Thu, 2009-03-26 at 17:02 -0700, dj wrote:
> Hello All,
>
> I would like to know if there is a way to exclude fields from an
> object query.
>
> For example:
>
> Suppose I have this model.
>
> class Person(Model.model):
> name = Charfield()
> address = Charfield()
> state = Charfield()
> etc.... I do realize my syntax is probably wrong, please for
> give me.
>
> I want to create a query that will return the state attribute for each
> object in the model.
> I found a reference to a method called hide in the django
> documentation.

No you didn't. You saw it in the initial proposal for a ticket (#5420),
but that isn't what was eventually committed. You have to read down to
the end of the ticket to see what made it into the code. The
documentation only talks about defer() and only().

That feature is in django 1.1-beta-1, but nothing earlier, so you would
have to be using the latest code.

However, ask yourself if you really need this. The answer is almost
certainly "no". If you only want particular values from a model, use the
values() method on a queryset and you'll get back only those values.
Alternatively, just get the whole model back. The use-cases for needing
to defer the loading of particular fields is very, very small. Names,
addresses and so forth are not going to be in that use-case.

Regards,
Malcolm

Reply all
Reply to author
Forward
0 new messages