python - Django Query only one field of a model using .extra() and without using .defer() or .only() -


i'm using django orm's exact() method query selected fields set of models save ram. can't use defer() or only() due constraints on orm manager using (it's not default one).

the following code works without error:

 q1 = model.custom_manager.all().extra(select={'field1':'field1'})  # want 1 field model 

however, when jsonify q1 queryset, every single field of model.. extra() must not have worked, or doing wrong?

print safestring(serializers.serialize('json', q1)) >>> '{ everything!!!!!}' 

to more specific, custom manager using django-sphinx. model.search.query(...) example.

thanks.

so, im not sure if can want do. however, if want values particular field or few fields, can values

it full query, result have values want. using example:

q1 = model.custom_manager.values('field1', 'field2').all() 

this should return valuesqueryset. not able use serializers.serialize have this:

from django.utils import simplejson data = [value value in q1] json_dump = simplejson.dumps(data) 

another better solution query intended, forgetting extra , values , use fields kwarg in serialize method this:

print safestring(serializers.serialize('json', q1, fields=('field1', 'field2'))) 

the downside none of these things same thing defer or only(all fields returned database), output desire.


Comments

Popular posts from this blog

javascript - DIV "hiding" when changing dropdown value -

Does Firefox offer AppleScript support to get URL of windows? -

android - How to install packaged app on Firefox for mobile? -