Re: Creating multiple related objects to a given QuerySet without evaluating the QuerySet
Alright, thanks Daniel.
On Jan 5, 1:12 am, Daniel Roseman <dan...@roseman.org.uk> wrote:
> 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.
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home