2013-11-15 100 views
1

我想了解如何在Django中使用inlineformset_factory創建的特定域上對內聯表單進行排序。我知道可以添加一個允許用戶指定排序順序的'order'列,但是我已經有了一個我想排序的字段。如何在Django中對一組內聯表格進行排序

LumFormSet = inlineformset_factory(Project, Luminaire, can_delete=True, 
    fields=('lumref', 'lummodel', 'manufacturer', 'lamptype')) 

生成formset類。

lumformset = LumFormSet(instance=project) 

生成該實例的formset。

我想排序lumref字段(這是一個十進制字段)。

+0

(在[11874])修正[#10263](https://code.djangoproject.com/ticket/10263) - 增加了一個BaseInlineFormSet的queryset參數,使用BaseModelFormSet標準化,並允許內聯自定義排序/子集。感謝Paulo Scardine的補丁。 –

回答

1

我們可以提供自定義的queryset我們InlineFormSets:

>>> custom_qs = queryset=Book.objects.order_by('-title') 
>>> formset = AuthorBooksFormSet(instance=author, queryset=custom_qs) 
>>> for form in formset.forms: 
...  print form.as_p() 
<p><label for="id_book_set-0-title">Title:</label> <input id="id_book_set-0-title" type="text" name="book_set-0-title" value="Les Fleurs du Mal" maxlength="100" /><input type="hidden" name="book_set-0-author" value="1" id="id_book_set-0-author" /><input type="hidden" name="book_set-0-id" value="1" id="id_book_set-0-id" /></p> 
<p><label for="id_book_set-1-title">Title:</label> <input id="id_book_set-1-title" type="text" name="book_set-1-title" value="Le Spleen de Paris" maxlength="100" /><input type="hidden" name="book_set-1-author" value="1" id="id_book_set-1-author" /><input type="hidden" name="book_set-1-id" value="2" id="id_book_set-1-id" /></p> 
<p><label for="id_book_set-2-title">Title:</label> <input id="id_book_set-2-title" type="text" name="book_set-2-title" value="Flowers of Evil" maxlength="100" /><input type="hidden" name="book_set-2-author" value="1" id="id_book_set-2-author" /><input type="hidden" name="book_set-2-id" value="5" id="id_book_set-2-id" /></p> 
<p><label for="id_book_set-3-title">Title:</label> <input id="id_book_set-3-title" type="text" name="book_set-3-title" maxlength="100" /><input type="hidden" name="book_set-3-author" value="1" id="id_book_set-3-author" /><input type="hidden" name="book_set-3-id" id="id_book_set-3-id" /></p> 
<p><label for="id_book_set-4-title">Title:</label> <input id="id_book_set-4-title" type="text" name="book_set-4-title" maxlength="100" /><input type="hidden" name="book_set-4-author" value="1" id="id_book_set-4-author" /><input type="hidden" name="book_set-4-id" id="id_book_set-4-id" /></p> 
>>> data = { 
...  'book_set-TOTAL_FORMS': '5', # the number of forms rendered 
...  'book_set-INITIAL_FORMS': '3', # the number of forms with initial data 
...  'book_set-0-title': 'Les Fleurs du Mal', 
...  'book_set-1-title': 'Le Spleen de Paris', 
...  'book_set-2-title': 'Flowers of Evil', 
...  'book_set-3-title': 'Revue des deux mondes', 
...  'book_set-4-title': '', 
... } 
>>> formset = AuthorBooksFormSet(data, instance=author, queryset=custom_qs) 
>>> formset.is_valid() 
True 
>>> custom_qs = queryset=Book.objects.filter(title__startswith='F') 
>>> formset = AuthorBooksFormSet(instance=author, queryset=custom_qs) 
>>> for form in formset.forms: 
...  print form.as_p() 
<p><label for="id_book_set-0-title">Title:</label> <input id="id_book_set-0-title" type="text" name="book_set-0-title" value="Flowers of Evil" maxlength="100" /><input type="hidden" name="book_set-0-author" value="1" id="id_book_set-0-author" /><input type="hidden" name="book_set-0-id" value="5" id="id_book_set-0-id" /></p> 
<p><label for="id_book_set-1-title">Title:</label> <input id="id_book_set-1-title" type="text" name="book_set-1-title" maxlength="100" /><input type="hidden" name="book_set-1-author" value="1" id="id_book_set-1-author" /><input type="hidden" name="book_set-1-id" id="id_book_set-1-id" /></p> 
<p><label for="id_book_set-2-title">Title:</label> <input id="id_book_set-2-title" type="text" name="book_set-2-title" maxlength="100" /><input type="hidden" name="book_set-2-author" value="1" id="id_book_set-2-author" /><input type="hidden" name="book_set-2-id" id="id_book_set-2-id" /></p> 
>>> data = { 
...  'book_set-TOTAL_FORMS': '3', # the number of forms rendered 
...  'book_set-INITIAL_FORMS': '1', # the number of forms with initial data 
...  'book_set-0-title': 'Flowers of Evil', 
...  'book_set-1-title': 'Revue des deux mondes', 
...  'book_set-2-title': '', 
... } 
>>> formset = AuthorBooksFormSet(data, instance=author, queryset=custom_qs) 
>>> formset.is_valid() 
True