A Django custom command to write model fields on an Excel file

Suppose you need to write down your model fields on an Excel file, for example to complement the documentation of your code.

Django has built in functions to introspect models and fields of an app, and you can leverage this API to have the information you need.

Read More »

Advertisements

A simple Python email gateway

Say you have a Django web application that you want to integrate with emails to make it possibile to send data and files to your web application over SMTP.

The good news is that Python has a simple SMTP daemon in the standard library, together with modules to parse emails. Let’s see how to create a simple email gateway on top of these tools.

Read More »

How to deploy a Django project in 15 minutes with Ansible

In this tutorial I will assume that you are a Django developer and you have built and tested a project locally. It’s time to deploy the project on a public server to let users access your awesome application.

So you need a VPS with an SSH access, then you will access the server, install and configure all necessary software (web server, application server, database server), create a database user, configure Django to use it, copy your Django project on the server, migrate the database, collect static files, trial and error, fix, trial and error, …

All this boring stuff will take some good hours that you should definitely spend in a more profitable way, don’t you think? The good news is that you can automate almost all the work needed to go from a vanilla VPS to a fully deployed server hosting your Django project.

Follow this tutorial and I’ll show you how to leverage the power of Ansible to automate all the needed steps in 15 minutes. Are you ready? Check the time on your clock and follow me!

Read More »

Drop all tables in a database

Sometimes it can be useful to completely drop all tables in a database, for example to reset a DB to a previous version from a backup.

After checking that you are doing this on the right database and that you know what you are doing, you can do this:

SET FOREIGN_KEY_CHECKS = 0;
SET GROUP_CONCAT_MAX_LEN=32768;
SET @tables = NULL;
SELECT GROUP_CONCAT('`', table_name, '`') INTO @tables
 FROM information_schema.tables
 WHERE table_schema = (SELECT DATABASE());
SELECT IFNULL(@tables,'dummy') INTO @tables;

SET @tables = CONCAT('DROP TABLE IF EXISTS ', @tables);
PREPARE stmt FROM @tables;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
SET FOREIGN_KEY_CHECKS = 1;

All credits goes to this guy on Stackoverflow.