djangosnippets.org: Latest snippets tagged with 'delete'http://djangosnippets.org/tags/delete/2016-01-06T19:37:09.149179-06:00delete object from table in form
2016-01-06T19:37:09.149179-06:00beckastarhttp://djangosnippets.org/snippets/10548/<p>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. </p>
Freely redistributablePre-delete signal function for deleting files a model
2012-09-19T23:27:40.846091-05:00mindcruzerhttp://djangosnippets.org/snippets/2820/<p>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 …</p>
Freely redistributableFile deletion of FileField and ImageField in the Admin panel
2012-07-04T12:32:55.339980-05:00davmuzhttp://djangosnippets.org/snippets/2782/<p>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.</p>
Freely redistributableRemoving old ImageFields and FileFields when updating through admin
2012-05-04T06:03:10.000027-05:00alejandro.alonsohttp://djangosnippets.org/snippets/2742/<p>Example:</p>
<p>admin.site.register(YourCoolModel, CustomModelAdmin)</p>
Freely redistributableDjango Admin Replacer Code
2010-11-18T01:36:14.842657-06:00riccardodivirgiliohttp://djangosnippets.org/snippets/2269/<p>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.
</p>
<p>Then i have added a form with a modelChoiceField to make you select another model instance when you are selecting …</p>
Freely redistributableResizeImageField
2010-09-29T17:49:55.335600-05:00wimhttp://djangosnippets.org/snippets/2212/<h1>ResizeImageField</h1>
<h1>(extension of RemovableImageField)</h1>
<p>by Wim Feijen, Go2People.</p>
<h2>What does it do?</h2>
<p>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.</p>
<p>It's …</p>
Freely redistributableOverride QuerySet.delete() (one way of preventing cascading deletes)
2010-01-07T10:23:30.876581-06:00timbroderhttp://djangosnippets.org/snippets/1851/<p>We needed to override the default QuerySet delete function to deal with a client problem that we were facing</p>
<p>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 …</p>
Freely redistributablefast table flush without raw SQL
2009-12-21T17:56:20.017930-06:00dsblankhttp://djangosnippets.org/snippets/1840/<p>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.</p>
Freely redistributableClear nullable foreign keys on delete
2008-12-02T22:47:48.720492-06:00psagershttp://djangosnippets.org/snippets/1231/<p>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 …</p>
Freely redistributableDelete View
2008-07-27T14:45:32.840671-05:00alcideshttp://djangosnippets.org/snippets/920/<p>Usage:
</p>
<p>@login_required
def action_delete(request,object_id):
return delete_view(request,object_id,ActionItem)
</p>
Freely redistributableDelete python compiled files (*.pyc)
2008-07-21T05:59:11.105097-05:00kogakurehttp://djangosnippets.org/snippets/899/<p>This simply deletes all compiled python files in a folder.</p>
Freely redistributableClear session data on login and logout
2008-04-03T10:57:41.329092-05:00jb0thttp://djangosnippets.org/snippets/681/<p>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 …</p>
Freely redistributableAdd delete button in admin cp
2008-03-28T04:46:42.150543-05:00cschandhttp://djangosnippets.org/snippets/671/<p>Add delete button on admin control panel to make delete as easy</p>
Freely redistributableUpdated FileField / ImageField with a delete checkbox
2008-03-10T10:56:37.910207-05:00tomZhttp://djangosnippets.org/snippets/636/<p>Example model:</p>
<pre><code>class MyModel(models.Model):
file = RemovableFileField(upload_to='files', \
null=True, blank=True)
image = RemovableImageField(upload_to='images', \
null=True, blank=True)
</code></pre>
<p>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 …</p>
Freely redistributableMultiple Delete in Admin
2008-03-02T16:36:33.818876-06:00gfranxmanhttp://djangosnippets.org/snippets/626/<p>This snippet demonstrates the use of the SpecialOps patch ( <a href="http://hackermojo.com/mt-static/archives/2008/02/django-special-ops.html">link here</a> )to the django 0.96 admin. Once you have the patch, adding actions like these is simple. </p>
<p>You can use the @admin_action decorator on model and manager methods to expose them inside the admin. For manager methods, the method …</p>
Freely redistributable