Tag Archives: Python

Django – Show model objects in Django Admin and apply format filter

We can easily scaffold the persisted objects list, add and delete pages on Django Admin by registering your model in your app admin.py file. For example, an app named eureka with model Person.

eureka/models.py

from django.db import models

class Person(models.Model):
  name     = models.CharField(max_length=256)
  homepage = models.URLField(max_length=256)

  def __unicode__(self):
    return self.name

 

eureka/admin.py

from django.contrib import admin
from eureka.models import Person

admin.site.register(Person)

Continue reading Django – Show model objects in Django Admin and apply format filter

Python – Execute shell command

Example run ls -l

import subprocess

# Execute ls -l
output = subprocess.Popen(["ls", "-l"], stdout=subprocess.PIPE).communicate()[0]

# Print the stdout
print output

 

Done =)

Reference:

Python – cx_Oracle throws missing libclntsh.so.11.1 when executed by django-chronograph

I have setup the cx_Oracle and i could query the data thru the custom django-admin command.

 

But when i try to schedule the custom command using django-chronograph. It throws the following error.

Traceback (most recent call last):
  File "query.py", line 7, in ?
    import cx_Oracle
ImportError: libclntsh.so.11.1: cannot open shared object file: No such file or directory

Continue reading Python – cx_Oracle throws missing libclntsh.so.11.1 when executed by django-chronograph

Python – Install cx_Oracle

I want to query data from an Oracle database from a Python program and this could be done by using the cx_Oracle Python package. This package required some Oracle package pre-requisites so it cannot be installed thru pip directly.

The following steps are done on a Centos 7 machine with Python 2.7.

1. Download and install the following from Oracle.

  • oracle-instantclient11.2-basic
  • oracle-instantclient11.2-devel
  • oracle-instantclient11.2-sqlplus

 

2. The installed client should be located at /usr/lib/oracle/11.2/client64.
Continue reading Python – Install cx_Oracle

Django REST framework – Create endpoints for custom actions

We have talked about how to use Django REST framework to create a RESTful backend for model CRUD.

 

But sometimes we would like to have some backend endpoints for non-model actions. Here is a very good article written by Jeremy Satterfield about non-model endpoints on Django REST framework.

 

The Django REST framework offer us a few ways to implement the web service. Some of them are very convenience to use but in return giving you less flexibility. The following example will make use of the APIView class to create a custom GET request end point.
Continue reading Django REST framework – Create endpoints for custom actions

Django – Schedule django-admin command using django-chronograph

Previously we talked about setting up some custom command in a Django project.

 

I would like to run the custom command in a regular interval. I could use Linux cronjob but it’s hard to manage and check the run history. It would be great if i could setup and manage these scheduled commands on the Django web portal.

django-chronograph, written by wnielson, is a Python Package which allows us to schedule any django-admin command through the web interface. It works well with the UTC timezone but if you have set the TIME_ZONE in your Django project setting.py, there would be time discrepancy.

For more detail about the bug, you can refer to the Issue #36 – Job doesn’t follow the Django’s TIME_ZONE setting.
Continue reading Django – Schedule django-admin command using django-chronograph

Django – Create custom django-admin command

We could manage our Django project by executing some commands which could be invoked through the manage.py. For example:

# Sync database
python manage.py syncdb

# Start the webserver
python manage.py runserver 0.0.0.0:8000

 

Not only this built-in commands, we can write our own command too.

1. Create a new Django project and in this example i called it custom_command. (Django 1.6.5)

django-admin.py startproject custom_command

Continue reading Django – Create custom django-admin command

Install Graphite under pyenv virtualenv on Ubuntu

It took me one and an half day to get the Graphite working…

1. Make sure you have pyenv and pyenv-virtualenv installed.

 

2. Install the following packages.

sudo apt-get install python-dev pkg-config libcairo2-dev memcached

 

3. Install Python 2.7 with enable-shared.

env PYTHON_CONFIGURE_OPTS="--enable-shared" pyenv install 2.7.8

Continue reading Install Graphite under pyenv virtualenv on Ubuntu

Django – Setup Django REST framework

We have the Django welcome page working already.

 

I would like to create some REST services on that setup. We could make use of the Django REST framework.

Let’s keep working on our <project_root>/django_poc project to manage articles by REST. The example in this post are based on the Django REST framework quickstart tutorial. It’s good to go through all of them which could give you a more thorough idea on how the REST framework works.

1. Install the Django REST framework in your virtualenv.

pip install djangorestframework

Continue reading Django – Setup Django REST framework