On Fri, Oct 31, 2008 at 4:37 AM, Tonne <[EMAIL PROTECTED]> wrote: > > I have spent hours looking in the docs and one this list for an answer > to this problem: > > I have a model that has, for example, 20 fields. > > On my site's homepage, where I'd like to offer a preview version of > the object, I'd need to retrieve only say, half of those fields to be > displayed. > > My dilemma is whether to simply use a Model.objects.all() query and > ignore the unnecessary data in the template, or find a more efficient > way to call only the data I need. > > Perhaps my model design is flawed and I should separate out the data > that is not common to the list and detail version of the content, or > perhaps I should have a model method that returns only the data fields > I need, etc.
There is nothing wrong with having a model with 20 fields if you keep the DRY (Don't Repeat Yourself) principle. I mean, if you are not sharing a subset of those fields with another model then it is ok. I think you need the values() method. It receives the fields you want to get as positional parameters (restricting the select sql query only to those fields). You can read the docs http://docs.djangoproject.com/en/dev/ref/models/querysets/#values-fields for further information. Regards. -- Marcelo Ramos --~--~---------~--~----~------------~-------~--~----~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---