I'm writing the custom modelformset. I need that forms to be sorted by value
of field "ordering". I overloaded __iter__ and __getitem__ methods of
BaseFormSet in my child formset class.
*My code:*
class SortedCatForms(BaseFormSet):
def __iter__(self):
print '__iter__'
return iter(
sorted(self.forms, key=lambda form:
form['ordering'].value()))
def __getitem__(self, index):
print '__getitem__'
return self.forms[index]
OrderCatsFormSet = modelformset_factory(ParentCategory,
fields=('category', 'ordering'),
formset=SortedCatForms,
extra=0)
*OrderCatsFormSet() is empty list. Can't get what the cause of it.*
I traced it and have some results:
When I comment out my custom formset it works:
OrderCatsFormSet = modelformset_factory(ParentCategory,
fields=('category', 'ordering'),
# formset=SortedCatForms,
extra=0)
After, I sorted it and make iterator object:
forms = OrderCatsFormSet().forms
def __iter__():
...: return iter(sorted(forms, key=lambda form:
form['ordering'].value()))
for i in __iter__():
...: print i
all my forms is ok...
But when I try to use my modelformset with prints (it is in my code above),
__iter__ method not prints.
How to fix it? And, what the cause of the problem?
*Thanks a lot! *
--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To view this discussion on the web visit
https://groups.google.com/d/msg/django-users/-/1pbENTybdSYJ.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en.