tags:

views:

23

answers:

3

Hi all,

Here is my model:

class Members(models.Model):
    firstname         = models.CharField(max_length=30)
    lastname          = models.CharField(max_length=30)
    gender            = models.CharField(max_length=1)
    email             = models.EmailField()
    password          = models.CharField(max_length=30)
    country_code      = models.CharField(max_length=4, choices=COUNTRY_CHOICES)
    zip               = models.CharField(max_length=10)
    will_share        = models.IntegerField()
    will_chat         = models.IntegerField()
    priv_level        = models.IntegerField()
    email_format      = models.CharField(max_length=4, choices=EMAIL_CHOICES)
    created           = models.DateTimeField(auto_now_add=True)
    modified          = models.DateTimeField(auto_now=True)
    last_login        = models.DateTimeField(auto_now=True)
    active            = models.BooleanField()

When I do a query as follows:

Members.objects.all()

How do I limit the fields returned. I only want firstname, lastname and last_login so effectively creating a query like:

SELECT firstname, lastname, last_login FROM members

There are a lot of members and I need to send a lot of XML data to the browser and don't want all the fields returned as it will bulk out the data too much. I have checked all the docs and can't see which option to use.

Is there an easy way for me to turn my limited fields dataset into XML for return to the browser to use in ajax?

Thanks

Rich

+1  A: 
  1. QuerySet.values() will let you specify the fields you want, but then the query will no longer return model instances.

  2. Serialization. But of course if you use the previous method then you might not be able to serialize it the same way. Consider using JSON instead.

Ignacio Vazquez-Abrams
I used the Members model in my question to make it easier to ask. I actually need XML as I am using Jquery jsTree to turn an adjacency list of hiearchical data into the tree. It supports XML transformation of flat data into the tree based on parent IDs but not json. It seems my options are limited as .only and .defer don't serialize the fields. Perhaps I will have to write a raw query. I wonder if that would work.
Rich
+1  A: 

You can limit the select itself with .only(fields) or .defer(fields) set on your queryset. This will return models that will execute individual selects to populate the missing fields if you access them.

Can't answer the XML serialization part, though.

Ben Jackson
It's odd there isn't a real way to get a simple list of fields in the result set. When I serialise a .only limited queryset it lists all the SB fields in it. It doesn't actually list my fields I .only-ed. It seems to be designed to work well for server side only.
Rich
A: 

I found the answer in the end at http://docs.djangoproject.com/en/dev/topics/serialization/

It's possible to limit the fields returned in the actual serialisation!

from django.core import serializers data = serializers.serialize('xml', SomeModel.objects.all(), fields=('name','size'))

Rich