views:

17

answers:

1

I'm building a page where the users can choose which rows to edit. After they select their rows and hit "edit" I'd like to present them with a modelformset_factory showing an editable version of all the rows the user selected.

My problem is I believe I need to turn this list of primary keys that I get back into a queryset suitable for use with the modelformset_factory and I don't know how to do that.

I suppose I could "brute force" it by specifying an SQL statement like:

SELECT <cols> FROM <table> WHERE pk=val1 OR pk=val2 OR pk=val3 OR ... OR pk=valN

But that just seems ugly.

Is there a way I can manually create a queryset by adding a bunch of essentially unrelated rows (all from the same table)?

+2  A: 

If you have a list of ids, you can simply query that the id is in your list of ids:

ids = [17, 23, 1492]  # (for example)
rows = Rows.objects.filter(id__in=ids)
Ned Batchelder
Excellent!!! I'm still navigating my way around the Django documentation. I see "__in" is described there.
jamida