Category Archives: Django

Django – Sort the Djano Admin list table by specific field

We could display our model objects on the Django Admin.

 

The list table could be sorted by specific column. Say we have added the creation date to the Person model class.

models.py

from django.db import models

class Person(models.Model):
  name       = models.CharField(max_length=256)
  homepage   = models.URLField(max_length=256)
  created_at = models.DateTimeField(auto_now_add=True)
  updated_at = models.DateTimeField(auto_now=True)

  def __unicode__(self):
    return self.name

Continue reading Django – Sort the Djano Admin list table by specific field

Django – Automatic creation date and update date

In your model class, we can set the auto_now_add and auto_add in the DateField or DateTimeField for creation date and update date.

from django.db import models

class Person(models.Model):
  name       = models.CharField(max_length=256)
  homepage   = models.URLField(max_length=256)
  created_at = models.DateTimeField(auto_now_add=True)
  updated_at = models.DateTimeField(auto_now=True)

  def __unicode__(self):
    return self.name

 

The auto_now_add will make the field read only while the auto_now will refresh the the field value when a .save() is executed.

Done =)

Reference: Django documentation – DateField

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

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

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