sehmaschine/django-grappelli — pull request #503 by aidaho (2014-07-31 14:58:49+00:00)
Title: Change 'formsetsort' template filter formset handling

VERSIONS: Grappelli master, Django 1.6.5
STATICFILES: OK
JAVASCRIPTS: OK
CUSTOMIZATIONS: NONE

Hi!

I have stumbled on a problem with a 'formsetsort' filter. I've been trying to order inline instances of a model which does not in fact have any ordering field by using a custom modelform where this field is present and populated programmatically as a default field value.

This worked nicely overall except instnaces were displayed unordered after the page was refreshed.
After some examination I found that 'formsetsort' filter was responsible for this.

I have a two improvements to suggest:
1. instead of checking BoundField.data atribute, call .value() method. The former thechnique yields nothing for additional modelform fields until validaiton (not sure of exact point of time) occurs. This also eliminates str->int conversions since .value() takes care of that.

2. tell forms bound to saved and new instances (extra inlines) by checking the presence of the instance primary key. No need to guess from ordering field content.

Regards,
Sergey.