On May 14, 2:37 pm, Jani Tiainen <rede...@gmail.com> wrote:
> Hi,
>
> I have in my database quite a bunch of a models where I do have quite
> large fields and I'm using .only('pk', 'identifier') to fetch only those
> two fields - mainly to make serverside natural sort for a data. Database
> is legacy one and it's being used with other external programs as well
> so changing it is not possible.
>
> After sorting I just need to get slice of data and fetch rest of fields
> in one query. I haven't found simple way to fetch rest of the fields in
> one request.
>
> So what I would like to do:
>
> qs = MyModel.objects.all().only('pk', 'identifier')
>
> list_of_models = sort_naturally(qs, 'identifier')
>
> sublist_of_models = list_of_models[10:20]
>
> for model in sublist_of_models:
>      model.fetch_all_deferred_fields()
While not totally same, how about:
for model in MyModel.objects.all(pk__in=[obj.pk for obj in
sublist_of_models]):
or maybe this will work, too:
for model in MyModel.objects.all(pk__in=sublist_of_models):

You will lose the sorting, so you will need to resort the models
again. If your model instances are changed, you will lose those
changes.

If the above isn't enough the best you can do currently is to create a
helper method which will do the merge manually.

 - Anssi

-- 
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+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to