Skip to content


Subversion checkout URL

You can clone with
Download ZIP
A script to post Django model instances to Twitter.
Branch: master

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.


This is a little script to have objects posted to Twitter from Django.


  1. Put the file somewhere in your project. I like to use a "dependencies" folder for stuff like this.

  2. Make a new app at (make sure you give it read and write privileges) and add the following variables to your file.

    TWITTER_CONSUMER_KEY = "--------------------"
    TWITTER_CONSUMER_SECRET = "-----------------------------------------"
    TWITTER_ACCESS_TOKEN_KEY = "-------------------------------------------------"
    TWITTER_ACCESS_TOKEN_SECRET = "-------------------------------------------"
  3. Add a post_save signal and an is_tweeted field to your model. Here's an example model:

    from django.db import models
    from dependencies.twitter_update import post_to_twitter  
    class MyModel(models.Model):
        text = models.CharField(max_length=255)
        link = models.CharField(max_length=255)
        is_tweeted = models.BooleanField()
        def __unicode__(self):
            return u'%s' % self.text  
        def get_absolute_url(self):
        # the following method is optional
        def get_twitter_message(self):
            return u'my-custom-twitter-message: %s - %s' % (self.text,  
    models.signals.post_save.connect(post_to_twitter, sender=MyModel)

    The is_tweeted field helps the script determine if the model has been tweeted already, avoiding any attempts to post duplicates. You can set a custom twitter status for the model with the get_twitter_message method. If this isn't set, it will just return the __unicode__ of the object. The post_save signal invokes the post_to_twitter script anytime a model instance is saved.


This script will detect if you're using django-shorturls and use those for the Tweet. I highly recommend it, django-shorturls is awesome. If you're not using it, the script will get a TinyURL instead.


Most of the code came from Arthur Furlan's code found in this snippet.

Something went wrong with that request. Please try again.