Ponytech

Technology for ponies

Apr 29, 2013

Save a model without triggering a signal nor the save() method

It happens you may want to save one of your model but without triggering any pre_save / post_save signal, nor the save() method of your model. Reasons could be, your are restoring some fixtures data, you don't want to update the object modification date that is configured with auto_now = True, you want to skip some validations, etc.

There had been a ticket opened in the past to request such a feature for Django but this request had been rejected.

So here is a quick tip to allow you to achieve this behavior if you really have to : you can use django bulk update feature. Because this function only performs a raw SQL query its does not actually knows what object you are updating no signals and no save() methods are triggered / called.

Let say you have the following model with a DateTimeField, auto_now set to True:

class Document(models.Model)
   title = models.CharField(max_length=255)
   last_modified = models.DateTimeField(auto_now=True)

To update the title of a Document without updating his last_modified date you can do:

doc = Document.objects.get(title='Old title')
Document.objects.filter(id=doc.id).update(title='New title')

Mar 11, 2013

Python integrated SMTP server

When developping web applications one often needs to send mails. It could be for example adress validation, notifications, or whatever. An underrated feature I wanted to share today is the python integrated SMTP server which could be a really good help in testing your application's mail features. Instead of having to deploy an SMTP server or you use your ISP's one, you can just type in your terminal :

python -m smtpd -n -c DebuggingServer localhost:25

This will start a local SMTP server listenning on port 25 (the default). This SMTP server will just print to stdout the mails it receive, along with headers, making your debugging easy.

Please note, that listenning to port 25 requires root privileges (it usually means to prepend sudo to the command). If you don't have such privileges on the machine you are developping you can listen on any other port superior to 1024. You then have to configure your application the use an SMTP non-default port. For example on django you would put in your settings.py :

EMAIL_PORT=2525

← Previous Page 2 of 2