The main purpose of the height_field and width_field attributes is to
give a performance boost in some situations,
these fields acting as a cache on the dimensions of the image.

I recently have switched-on those attributes for a newspaper
publishing project where we manipulate some huge .tiff pictures
because there were big overloads and very slow responses for some
simple GET views displaying the image dimensions due to a sytematic
recalculation via the PIL.
Turning on width|heigh_field has dramaticly improved the performance
for these views. But as a side-effect, the POST context have taken the
absolute opposite direction in terms of responsiveness.

After digging a bit with pdb I found out that the
ImageFileDescriptor.__set__(self, instance, value) call in the form
(formset in my case) validation process was the reason.
When the dimension_fields are presents, they are filled-in with the
following call line 314 in db/models/fields/files.py :

                    self.field.update_dimension_fields(instance,
force=True)

where nothing happens when the fields are not presents (because we
don't need the dimensions of forms containing images that have not
changed).

Here a simple use-case to illustrate :

models.py
--------------

class Person(models.Model):
    name = models.CharField(max_length=100, blank=True, null=True)


class ImagePerson(models.Model):
    person = models.ForeignKey('Person')
    image = models.ImageField('image', height_field='image_height',
width_field='image_width')
    image_height = models.IntegerField(blank=True, null=True)
    image_width = models.IntegerField(blank=True, null=True)


views/forms
----------------

class PersonForm(forms.ModelForm):
    class Meta:
        model = Person

class ImagePersonForm(forms.ModelForm):
    class Meta:
        model = ImagePerson
        exclude = ('image_height', 'image_width',)

ImagePersonFormSet = inlineformset_factory(Person, ImagePerson,
form=ImagePersonForm)


# The view is a extremly simplified show/edit with an inline_form.

def detail_person(request, person_id):
    person = Person.objects.get(pk=person_id)

    person_form = PersonForm(request.POST or None, instance=person)
    image_person_formset = ImagePersonFormSet(request.POST or None,
request.FILES or None,\ instance=person)

    if request.method == 'POST':
        pdb.set_trace()
        if image_person_formset.is_valid():
            image_person_formset.save()

    return render_to_response('my_app/detail_person.html', {
        'person_form': person_form,
        'image_person_formset': image_person_formset,
        }, context_instance=RequestContext(request))


 use case
-------------

The simple fact of posting an unchanged form of a person cause the
opening with the PIL of *all* the unmodified images to fill-in the
dimension_fields of the virtual models in the form validation process.



This looks like an unecessary overhead and maybe something more lazy
could be more appropriate. I am sorry for not coming with a patch -  I
do not get the whole picture of the core - but an expert point of
views is welcome here :-)

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers" group.
To post to this group, send email to django-developers@googlegroups.com.
To unsubscribe from this group, send email to 
django-developers+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-developers?hl=en.

Reply via email to