开发者

picking the rows to be in a queryset

开发者 https://www.devze.com 2023-01-21 11:59 出处:网络
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 a

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


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

精彩评论

暂无评论...
验证码 换一张
取 消