sehmaschine/django-grappelli — Issue #64: Inline sortables are broken when there is only a file field
Author: calebbrown
Created: 2011-12-14 05:23:21+00:00
State: open

### The Bug

We have the following model:

```python
class MyModel(models.Model):
parent = models.ForeignKey(OtherModel)
image = models.ImageField(upload_to='images')
ordering = models.PositiveIntegerField()
class Meta:
ordering = ['ordering']
```

With this admin code:

```python
class MyModelForm(forms.ModelForm):
ordering = forms.IntegerField(widget=forms.HiddenInput)
class Meta:
model = MyModel
fields = ('image', 'parent', 'ordering')

class MyModelInline(admin.TabularInline):
model = MyModel
form = MyModelForm
sortable_field_name = "ordering"
```

In this scenario, the inlines will only save the ordering data if a file has been chosen for upload.

Once the file has been uploaded and saved the ordering data won't be updated.

### Why?

This is due to the way grappelli is check to see if an inline has data and the way files are handled.

In Django admin a file field with a file set will look like this:

```html
Currently: images/myimage.jpg

Clear

Change:
```

Grappelli uses the following code to find all the fields that need to have sorting information updated:

```javascript
$("#images-group").find("div.tbody.dynamic-form").each(function(){
var fields = $(this).find("div.td :input[value]");
if (fields.serialize()) {
$(this).find("input[name$='"+sortable_field_name+"']").val(i);
i++;
}
});
```

Because the file field and the checkbox for clearing it are empty ```fields.serialize()``` is false, therefore the sorting information isn't updated.