List Info

Thread: How to save a FileField in a custom manipulor?




How to save a FileField in a custom manipulor?
user name
2006-11-21 14:35:24
Hi,

I created a custom manipulator for saving an object that has
a FileField, but
every piece of code I write returns a different error each
time (lots of typos
and misunderstanding I admit).

Can someone drop me the typical code to use in the
manipulator's save() method?
My latest attempt is something like:

**********************************************************

class HeadacheManipulator(forms.Manipulator):

def __init__(self, object):
    self.original_object = object
    self.fields=[forms.FileUploadField('upload')]

def save(self, new_data):
    self.original_object.myfile = new_data['upload']
    self.original_object.save()

**********************************************************

Currently this code returns an error ("Error binding
parameter 2 - probably
unsupported type") but I have attempted so many things
(and broke my app in so
many ways...) that I am not sure this is still caused by my
manipulator code.

I read the following sections of Django documentation but
could not find
anything:
- Model reference (FileField description)
- Forms and manipulators (and the specific paragraph about
FileFields)
- Request object (and the stuff related to request.FILES)

I ended up looking at the AutomaticManipulator. There I
found code that invokes
the 'save_file' method of each FileField... Some more
confusion to my brain.
What is this method used for?
(Harmless criticism: core Django code lacks doc strings in
many areas...)

Thanks for any info that could save my day.
Thierry.

--~--~---------~--~----~------------~-------~--~----~
 You received this message because you are subscribed to the
Google Groups "Django users" group.
To post to this group, send email to django-usersgooglegroups.com
To unsubscribe from this group, send email to
django-users-unsubscribegooglegroups.com
For more options, visit this group at htt
p://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

How to save a FileField in a custom manipulor?
user name
2006-11-22 09:16:18
To refine my problem, can someone criticize my code and help
me make it work?
I want to save my object with a FileField in a custom
Manipulator...

*** Model *********************************************

class Attachment(models.Model):
    file =
models.FileField(upload_to="attachments")

*** Manipulator ***************************************

class AttachmentManipulator(forms.Manipulator):
    def __init__(self):
        self.fields = [forms.FileUploadField('upload')]
    def save(self, new_data):
        new_object = Attachment(file=new_data['upload'])
        new_object.save()

*** View **********************************************

def view_attach(self):
    manipulator = manipulators.AttachmentManipulator()
    if request.POST:
        new_data = request.POST.copy()
        new_data.update(request.FILES)
        errors = manipulator.get_validation_errors(new_data)
        if not errors:
            manipulator.do_html2python(new_data)
            manipulator.save(new_data)
            return HttpResponseRedirect('/dummy_url/')
    else:
        errors = new_data = {}
    form = forms.FormWrapper(manipulator, new_data, errors)
    return render_to_response('my_template.html', {'form':
form})

*******************************************************

I know there must be something wrong in my manipulator's
save() method.
But I can't figure out what.
I have the correct encoding in the <form> tag of my
HTML template, and I have
both {{ form.upload }} and {{ form.upload_file }} as well.

I'm short of ideas to make it work...

--~--~---------~--~----~------------~-------~--~----~
 You received this message because you are subscribed to the
Google Groups "Django users" group.
To post to this group, send email to django-usersgooglegroups.com
To unsubscribe from this group, send email to
django-users-unsubscribegooglegroups.com
For more options, visit this group at htt
p://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

How to save a FileField in a custom manipulor?
user name
2006-11-22 13:31:16
Some progress on my side...
Ok now I understand that doing this in the manipulator
save() method is not
going to help:
new_object = Attachment(file=new_data['upload']) # I got it
wrong...

Following the code of AutomaticManipulator, my save() method
now looks like:
new_object = Attachment(file=new_data['upload']['filename'])
new_object.save()
new_object.save_file_file(...)

In other words, I guessed that I have to create the object
first, assign the
filename to the FileField, save the object, THEN save the
real file.

Am I correct? Because if I am, I have some more questions:
* what is the correct method to save the file?
  --> I see object.save_FIELD_file(...) will work, but I
am not sure about the
parameters.
  --> There is also a field.save_file(...) in my
manipulator (I checked the code
in AutomaticManipulator save method as well), but this looks
like black magic
and again, I don't know about the parameters (off-topic:
this is the place
where doc string could help)
* how do I represent my manipulator fields in the html
template? Following the
documentation, I wrote {{ form.upload }} and
{{form.upload_file }} but the
latter statement does not appear when I look at the
generated HTML source code.
* in my new_data MultiValueDict, the value of 'upload' (i.e
my manipulator only
field) is represented as an array of only one value, but I
have only one field
named 'upload' in my template. Any idea?

--~--~---------~--~----~------------~-------~--~----~
 You received this message because you are subscribed to the
Google Groups "Django users" group.
To post to this group, send email to django-usersgooglegroups.com
To unsubscribe from this group, send email to
django-users-unsubscribegooglegroups.com
For more options, visit this group at htt
p://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

[1-3]

about | contact  Other archives ( Real Estate discussion Medical topics )