many to many add by ids

186 views
Skip to first unread message

rich....@gmail.com

unread,
Sep 29, 2009, 7:16:04 PM9/29/09
to Django users
I have the following I'm trying to optimize.

Input (from the web) is

sids = "1,2,3,5" (a list of ids is posted, say they are article ids)

Now, to add them to a many to many, I'd like to just

try:
authors.articles.add(ids).
except:

I know the ids need to be ints, but tried a number of things to get
them into the proper format but all fail with one exception or another

ids = map(int, sids.split(','))

but i konw that: authors.articles.add(1,2,3,4) works....how do i get
"1,2,3,4" into 1,2,3,4

probably a simple python thing or something.

thanks
(disregard the need for validation above).

Tom Evans

unread,
Sep 30, 2009, 4:55:14 AM9/30/09
to django...@googlegroups.com

Yep, pretty straightforward:

authors.articles.add(*map(int, sids.split(',')))

See
http://docs.python.org/tutorial/controlflow.html#tut-unpacking-arguments

Cheers

Tom

Reply all
Reply to author
Forward
0 new messages