I am working on a project with the following:
class Person(models.Model):
# ...
picture = models.ImageField(...)
I'd like to give each Person the ability to delete images, and I'd
like to remove deleted pictures from the filesystem once a person has
deleted them. From what I can tell, the delete_file function in the
FileField class (http://code.djangoproject.com/browser/django/trunk/
django/db/models/fields/__init__.py, line 767) will do that for me.
However, I haven't gotten it to work. Here's a chunk of the code I'm
working with...
class PersonForm(forms.Form):
# ...
picture = forms.ImageField(required=False)
# ...
def save(self):
# ...
p = Person.objects.get(pk=pk)
file, content = (self.cleaned_data['picture'].filename,
self.cleaned_data['picture'].content)
# I'm most confused on how to get the instance of ImageField
class to operate on
# this is the best thing I could come up with
picfield = [f for f in p.__class__._meta.fields if f.name ==
'picture'][0]
picfield.delete_file(p)
p.save_picture_file(file, content)
p.save()
# return None
Django doesn't argue with the syntax, but it also won't delete the
file from the filesystem. My main assumptions are that:
delete_file accepts the instance of the model containing the
ImageField (in this example, Person)
I have the steps (get field, delete file, save new file, save
person instance) in the right order
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---