I had a difficult time understanding how to delete an item from a table within a template, using a modelform. I couldn't find a good example, so I wanted to post the code that ultimately worked.
This snippit is meant to be used with the pre_delete signal to delete any files associated with a model instance before the instance is deleted. It will search the model instance for fields that are subclasses of FieldFile, and then delete the corresponding files. As such, it will work with any model field that is a subclass of FileField.
This code runs well on Django 1.4 also with the Admin panel.
It's important to get the storage and the path before delete the model or the latter will persist void also if deleted.
Ok let's descrive what i have done
I subclassed the django admin to create a form that makes you choose if activate a delete and replace login inside your admin.
Then i have added a form with a modelChoiceField to make you select another model instance when you are selecting an istance to delete. If you select another instance the current instance will be replaced.
ResizeImageField
================
(extension of RemovableImageField)
=================================
by Wim Feijen, Go2People.
What does it do?
----------------
ResizeImageField is a replacement for django's ImageField. It has two major benefits:
1. Creation of thumbnails and scaled images.
1. Extends the image upload form and adds a preview and a checkbox to remove the existing image.
It's easy to use:
- Replace ImageField by ResizeImageField
- No further changes are necessary
Requirements:
-------------
Working installation of PIL, the Python Imaging Library
Usage
-----
- add resize_image to your app
- add resize_filters.py to your templatetags
- in settings.py, set a PHOTO_DIR, f.e. photos/original
- in models.py, add:
- from settings import PHOTO_DIR
- from resize_image import ResizeImageField
- photo = ResizeImageField(upload_to=PHOTO_DIR, blank=True)
Scaled images will be stored in 'photos/scaled',
thumbnails will be stored in 'photos/thumb'.
Access your images from your template. Add::
{% load resize_filters %}
{{ address.photo.url|thumb }}
or::
{{ address.photo.url|scaled }}
Defaults
-------
- Scaled images are max. 200x200 pixels by default
- Thumbnails are 50x50 pixels.
Override the default behaviour in settings.py
Scaling is done by PIL's thumbnail function, transparency is conserved.
Credits
------
This code is an adaptation from python snippet 636 by tomZ: "Updated Filefield / ImageField with a delete checkbox"
We needed to override the default QuerySet delete function to deal with a client problem that we were facing
Yes This is monkey-patching, and probably bad practice but if anyone needs to conditionally override the cascading delete that django does at the application level from a queryset, this is how to do it
Perhaps you don't want to drop a table, but you also want to do something faster than Model.objects.all().delete() but without resorting to raw SQL. This function, clear_tables, will call the sql_flush operation on a list of tables.
Django 1.0 is apparently hard-coded for cascading deletes. I find that I often have nullable foreign keys on models whose records must not be deleted along with those they refer to. I override Model.delete() in an intermediate base class and execute this method to clear out all nullable foreign keys before allowing a delete to proceed.
This was born as a result of the fact that session data is shared across logins on a single browser. If you login as user1 and session data is stored, then login as user2 the same session data will be available to your application. Please see the ticket who's validity is at this point in question. Some feel that this is normal behavior.
http://code.djangoproject.com/ticket/6941
I use this code in conjunction with
http://code.google.com/p/django-registration/
Place this code in registration.__init__ and change registration.urls to have login and logout route to the new alternate versions alt_login, alt_logout.
I have only been using Python and Django for a couple months now so I hope that this implementation is not too terrible. It works for me. Enjoy.
Example model:
class MyModel(models.Model):
file = RemovableFileField(upload_to='files', \
null=True, blank=True)
image = RemovableImageField(upload_to='images', \
null=True, blank=True)
A delete checkbox will be automatically rendered when using ModelForm or editing it using form_for_instance. Also, the filename or the image will be displayed below the form field. You can edit the render method of the DeleteCheckboxWidget or add one to RemovableFileFormWidget to customize the rendering.
UPDATE: 5. April 2009. Making it work with latest Django (thanks for the comments).
This snippet demonstrates the use of the SpecialOps patch ( [link here](http://hackermojo.com/mt-static/archives/2008/02/django-special-ops.html) )to the django 0.96 admin. Once you have the patch, adding actions like these is simple.
You can use the @admin_action decorator on model and manager methods to expose them inside the admin. For manager methods, the method should accept a list of IDs. For model methods, only self should be required.