On Jan 4, 11:40 pm, David <davidkazuh...@gmail.com> wrote:
> I have a QuerySet of Books, and I want to create an Action for each
> Book in that QuerySet. I want to avoid evaluating the Books QuerySet,
> but the only way I can think of doing what I want to do evaluates it.
> For example,
>
> def create_actions(books, action)
>     """" books is a QuerySet of Book"""
>     for book in books:
>         Action(book=book, action=action).save()
>
> any suggestions on how I can do this without evaluating books?

There's not really any way. Here's an optimisation, at least:

    book_ids = books.values_list('id', flat=True)
    for book_id in book_ids:
        Action.objects.create(book=book_id, action=action)

This only gets the list of book IDs from the database, so avoids
evaluating the complete queryset, but it still does the query to get
the ID list.
--
DR.

--

You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@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