djangosnippets.org: Latest snippets tagged with 'signals'http://djangosnippets.org/tags/signals/2022-12-19T11:51:50.631290-06:00Add custom fields to the built-in Group model
2022-12-19T11:51:50.631290-06:00jmoppelhttp://djangosnippets.org/snippets/10945/<p>Add fields and extend Django's built-in <code>Group</code> model using a <code>OneToOneField</code> (i.e. a profile model). In this example, we add a <code>UUIDField</code>. Whenever a new group is created, we automatically (via signals) create a corresponding <code>Role</code> record referencing the newly created group. Whenever a Group is deleted, the corresponding …</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 redistributableShow logged users - keeping track of users login and logout
2012-09-05T15:02:18.391108-05:00albertorcfhttp://djangosnippets.org/snippets/2808/<p>Showing a list of logged users using the <em>user_logged_in</em> and <em>user_logged_out</em> signals.</p>
<p>See <a href="https://docs.djangoproject.com/en/1.4/topics/auth/#login-and-logout-signals">login and logout signals</a> in Django docs.</p>
Freely redistributableBinding signals to abstract models
2012-05-15T11:25:45.770359-05:00andreterrahttp://djangosnippets.org/snippets/2749/<h2>Intro</h2>
<p>I found a question on SO for which Justin Lilly's answer was correct but not as thorough as I'd like, so I ended up working on a simple snippet that shows how to bind signals at runtime, which is nifty when you want to bind signals to an abstract …</p>
Freely redistributableUser Profile minimal code
2011-08-02T04:02:05.273049-05:00Natimhttp://djangosnippets.org/snippets/2501/<p>Usually I start an authentication app with this model.</p>
<p>Don't forget to set it up in the settings file</p>
<pre><code>AUTH_PROFILE_MODULE = 'authentication.UserProfile'
</code></pre>
Freely redistributableRun model validation before saving a model instance
2011-01-12T01:11:48.105528-06:00buriyhttp://djangosnippets.org/snippets/2319/<p>How to validate your model at save using the pre_save signal.
</p>
<p>from http://groups.google.com/group/django-developers/browse_thread/thread/eb2f760e4c8d7911/482d8fd36fba4596?hl=en&lnk=gst&q=problem+with+Model.objects.create#482d8fd36fba4596
</p>
Freely redistributableimmitating 'real' post_syncdb signal
2010-04-16T15:50:03.869210-05:00jangohttp://djangosnippets.org/snippets/1988/<p>I did not like the idea of having to load fixtures by creating a huge initial_data.json file. I also did not want to store my initial data in a bunch of <modelname>.sql files.</p>
<p>Django has post_syncdb signal which fires when model(s) for an application are installed, but I needed something …</p>
Freely redistributablemanually models unique_together check via signals
2009-07-13T07:27:11.376591-05:00jediehttp://djangosnippets.org/snippets/1628/<p>signal handler for checking unique_together manually.
</p>
<p>Also available via django-tools: http://code.google.com/p/django-tools/
</p>
Freely redistributableSignal to notify new saved comments
2009-05-31T17:29:55.122339-05:00arthurfurlanhttp://djangosnippets.org/snippets/1539/<p>Signal to notify new saved comments.</p>
<p><strong>Example:</strong></p>
<pre><code>from django.contrib.comment import models, signals
signals.comment_was_posted.connect(new_comment_notifier,
sender=models.Comment)
</code></pre>
Freely redistributableExecute a signal once
2009-05-05T15:58:02.712215-05:00johnnoonehttp://djangosnippets.org/snippets/1490/<p>Decorates signals for executing only one time</p>
<p>Exemple usage :</p>
<pre><code>from django.core.mail import EmailMultiAlternatives
from django.template.loader import render_to_string
from django.contrib.auth.models import User
@one
def user_welcome(sender, instance, created, **kwargs):
# Send a welcome email
if created == True and isinstance(instance, User):
instance.message_set.create(message=_(u"Ho, Welcome %s!" % instance))
subject, from_email, to = 'Welcome …</code></pre>
Freely redistributableCustomImageField for Django 1.0 alpha
2008-08-07T12:38:30.713523-05:00hanksimshttp://djangosnippets.org/snippets/949/<p>The venerable CustomImageField, invented by <a href="http://scottbarnham.com/blog/2007/07/31/uploading-images-to-a-dynamic-path-with-django/">Scott Barnham</a> and rejiggered for newforms-admin by <a href="http://pandemoniumillusion.wordpress.com/2008/08/06/django-imagefield-and-filefield-dynamic-upload-path-in-newforms-admin/#comments">jamstooks</a>.</p>
<p>This here is a stab at a <a href="http://code.djangoproject.com/wiki/BackwardsIncompatibleChanges#Signalrefactoring">post-Signals-refactor</a> version. Seems to do 'er.</p>
<p>Note: This should be pointless once <a href="http://code.djangoproject.com/ticket/5361">fs-refactor</a> lands.</p>
Freely redistributableGeneric Recievers For Signals
2008-06-04T16:20:00.228341-05:00ericmoritzhttp://djangosnippets.org/snippets/784/<p>Allows you to add args and kwargs to the signal reciever</p>
Freely redistributableAlter Column Lengths of Contrib Apps
2008-04-23T07:39:35.642743-05:00guettlihttp://djangosnippets.org/snippets/718/<p>If you want to modify the length of a column of a contrib application, you can either
modify django (cumbersome) or you can run a post_syncdb signal hook.</p>
<p>Related: <a href="http://code.djangoproject.com/ticket/4748">Ticket 4748</a></p>
Freely redistributablePreSaveMiddleware
2007-11-20T12:47:13.307587-06:00pterkhttp://djangosnippets.org/snippets/476/<p>With this middleware in place (add it to the MIDDLEWARE_CLASSES in your settings) you can pass a request to the model via a pre_save method on the model.</p>
<p>I'm not sure if it is an improvement over the [threadlocals method] (http://code.djangoproject.com/wiki/CookBookThreadlocalsAndUser) but it may be an alternative that can be …</p>
Freely redistributable