Let’s continue our example in
So now we have a working endpoint @ http://127.0.0.1:8000/custom/get/.
Continue reading Django REST framework – Setting permissions
Let’s continue our example in
So now we have a working endpoint @ http://127.0.0.1:8000/custom/get/.
Continue reading Django REST framework – Setting permissions
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
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
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
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
1. Deactivate the virtualenv if there is.
pyenv deactivate
2. Install the required libraries.
sudo apt-get install libpq-dev python-dev
3. Install PostgreSQL.
sudo apt-get install postgresql postgresql-contrib
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
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
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
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