On Sun, Jan 31, 2010 at 1:38 AM, chefsmart <moran.cors...@gmail.com> wrote:
> Let's say I have two models -  Article and Publication. Article has a
> field
>
> publications = models.ManyToManyField(Publication)
>
> Let's say I present the user with a series of checkboxes representing
> publications (much like the ModelMultipleChoiceField, but I am not
> using ModelForms here) and getting back selected publications with
>
> selectedPubs = map(int, request.POST.getlist('publications')) #getting
> the selected primary keys -- integers
>
> Then if I do
>
> for x in selectedPubs :
>    article1.publications.add(x)
>
> This seems not a good enough solution as far as performance and
> database interaction is concerned.
>
> What is a better way to handle a situation like this?

article1.publications.add(*selectedPubs)

will allow you to insert all the entries in selectedPubs into the m2m
relation in a single call. This doesn't quite reduce to 1 SQL call,
but it will be less SQL calls than multiple individual insertions.

Yours,
Russ Magee %-)

-- 
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