It appears that the usage of FileField with ModelForm is not working 
correctly. [1] is the ticket in Trac. The problem, put briefly, is that 
when a ModelForm is given an instance it is marked as required and 
requires the user to upload a file to bypass the validation. Keep in 
mind that if the FileField is explicitly marked as not required then 
this is a non-issue.

This is currently a bug in the newforms-admin that I have been working 
on for a little while now. After working alot on the formset refactor 
patch the problem really seems to lie in newforms itself.

This wasn't a problem with the form_for_* helper functions since 
form_for_instance knew the model data when creating the form class. 
That is why the FileField model class has the following code in its 
formfield method:

# If a file has been provided previously, then the form doesn't require
# that a new file is provided this time.
if 'initial' in kwargs:
    defaults['required'] = False

This will no longer work correctly because initial will never be passed 
in the kwargs since the form creation is now done in the 
ModelFormMetaclass. In looking at the FileInput widget tests it makes 
note that its data is useless due to the nature of how file uploads are 
handled. That makes complete sense, however, is causing this problem 
when the data is coming from the model and not the web browser.

To write a patch I created a new widget named BoundFileInput that 
inherits from MultiWidget to display the FileInput and HiddenInput 
widgets. The FileInput widget works the same as it does now. The 
HiddenInput widget maintains the data that came from the model. As I 
understand it this is how it used to work in oldforms, but perhaps just 
the admin in trunk.

This is where the problem comes in. To hook BoundFileInput widget into 
the correct place I found out that doing so in the newforms FileField 
__init__ method is still too soon to check for the initial data. This 
then made me realize that perhaps this actually had to be done in the 
BaseForm. It wouldn't seem correct to do this only in the ModelForm, 
but perhaps I am wrong.

I then figured that changing the widget based on the initial data in 
the BaseForm __init__ still seems very hackish, but I may be wrong 
again. I then remebered about BoundField which basically defines a 
field and its data. Well, that is only used for the display of the 
widget as opposed to both display and validation. This seems to be a 
gap that might need to be filled to accomplish this fix properly.

So, with all that said I ask:

1. Should FileInput be aware of its data since one use-case is valid.
2. How should the widget be handled when a validation error occurs?

Any ideas/ensight would be greatly appreciated.

[1]: http://code.djangoproject.com/ticket/6302

-- 
Brian Rosner
http://oebfare.com



--~--~---------~--~----~------------~-------~--~----~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to