Django Postgres Transaction Error Rollback
Every time there's an error in Django if using postgres, the connection somehow wasn't terminate properly. I have to manually rollback in my Django Shell. and rerun again
from django.db import connection connection._rollback()
Update: This bug has just been fixed in the latest development branch and soon will be release. Hopefully in Django 1.6 https://code.djangoproject.com/ticket/10813
Django Celery Apply task throughout the database objects
So how to apply a task throughout the database objects? You have to iterate through each of the database entries, and put the task into the queue.
Let's say we have the following task:
from celery import task
@task
def notify_user(user):
send_mail("Subject", "Body", None, [user.email,])
Instead of doing this: (putting the queue one by one)
users = User.objects.all()
for user in users:
notify_user.apply_async(user)
You could do this: (putting the queue in one block of group)
from celery import group users = User.objects.all() job = group(notify_user.s(user) for user in users) job()
Chainable Queryset
You can chain your query something like:
Apple.objects.fresh().red().big()
But of course these fresh, red, big must be a queryset method instead of manager methods.
Example:
from django.db import models
class AppleQuerySet(models.query.QuerySet):
def fresh(self):
return self.filter(fresh__gte=80)
def big(self):
return self.filter(weight__gte=100)
def red(self):
return self.filter(color='red')
class AppleManager(models.Manager):
def get_queryset(self):
return AppleQuerySet(self.model)
def __getattr__(self, attr, *args):
try:
return getattr(self.__class__, attr, *args)
except AttributeError:
return getattr(self.get_queryset(), attr, *args)
Django Class Based View Prefix for URL Dispatcher patterns
Have you wrote your class based views? If not, please do so as it makes your code looks better. Please refer to Django docs for more info.
In this blog post, I will share a tips on how to make use of view prefix of the url dispatcher patterns which wasn't address in the Docs.
Let's say we have the following View
# myapp/views.py
from django.views.generic import View
from django.http import HttpResponse
class MyView(View):
def get(self, request):
return HttpResponse("Hello World")
def another_view(request):
return HttpResponse("Hello World")
In the Django Documentation, here's how they do it.
# The Plain old way, which we used to write.
# View prefix is not possible without importing the module
# myapp/urls.py
from django.conf.urls import patterns, url
from myapp.views import MyView
urlpatterns = patterns('myapp.views',
url(r'^class-based-view-old/$', MyView.as_view()),
url(r'^function-view/$', 'another_view'),
)
Here's how I do it to make use of the view prefix on a class based view
# myapp/views.py
from django.views.generic import View
from django.http import HttpResponse
class MyView(View):
def get(self, request):
return HttpResponse("Hello World")
# This is to convert the class into a callable view
my_view = MyView.as_view()
# myapp/urls.py
from django.conf.urls import patterns, url
urlpatterns = patterns('myapp.views',
url(r'^class-based-view/$', 'my_view'),
)
Django Login User Pragmatically
Before you login, user has to be authenticated. It doesn't make sense if you allow someone to login without him keying his password.
Usually, the right timing to know his password before being encrypted to database is when a user submit POST request whereby the password is still in plain text. This is the right time for you to authenticate the user. If plain text successfully authenticated, then you can login the user. or else it will fail.
Authentication user is straight forwards, just pass in the username and password. But if you have the email instead. You need to query the database for the username. If you don't have the password or the password is encrypted in the database, then you're a bit out of luck, because you will not be doing it the easy way in which you need to patch the user.backend.
Otherwise, Here's how you login the username and password the easy way.
from django.contrib.auth import authenticate, login user = authenticate(username=username, password=password) login(request, user) # Redirect the user or use ajax or whatever to refresh the page.
reverse_lazy in django 1.3
try:
from django.core.urlresolvers import reverse_lazy
except ImportError:
from django.core.urlresolvers import reverse
from django.utils.functional import lazy
reverse_lazy = lambda *args, **kwargs: lazy(reverse, str)(*args, **kwargs)
Setting up PostgreSQL with PostGIS on Linux (Fedora)
yum install postgresql-server yum install postgresql yum install postgis service postgresql initdb service postgresql start chkconfig postgresql on sudo -u postgres psql > create user eugene createdb createuser password 'password'; createdb mydb CREATE EXTENSION postgis; CREATE EXTENSION postgis_topology;
If you encounter an error about something couldn't be found, read the troubleshooting guide carefully at the reference below especially the library environment section.
Further reference can be found at
Bash/Linux Remove File Recursively by extension pattern
If you want to remove all pyc file within your project.
rm -rf `find . -name '*.pyc'`
Restore Deleted File in Git
git rev-list -n 1 HEAD -- file/you/want/to/restore git checkout c0mm1t^ -- file/you/want/to/restore git commit -a
In English:
- Find the commit that you want to revert to. This commit should be the when the file exists. (usually the latest)
- Check out the file of that commit
- Add And Commit Back
Web Developer or Software Engineer
I'm going to highlight the difference between engineer and developer, in my opinion. This opinion may not applicable to all people, but rather it applies to all what I've seen.
Developer uses the best technology to solve problem. If a developer couldn't find the solution. They tends to said, we can't build it yet due to the lack of technology.
On the other hand, Engineer solve this problem when there's no solution, but the payoff is that they don't necessarily do it well as compared to a developer.
But these two fields can be merged. Such that A good developer knows a wide range of technology to use. And a good engineer know how it was built and how to build one when no solution ever made.
In my company, I've seen these two personality. One is Web developer and the other is Software Engineer. They tend to have a conflict where a developer thinks more about how maintainable it is in the long run or from the business point of view and what makes their life easier.
Engineer doesn't care about the current technology, they just build from their prior knowledge. This engineer tends to cause a conflict with the web developer. One area of conflict such as the maintainability issue. Developer doesn't want to read your code, they want to use the function. On the other hand, an Engineer doesn't have an issue with the developer because Engineer always said, "No problem, I can fix that"
So, now back to the question from your business.
Q1: Has your business ever built by other and has a common functionality with the other business? Then Web Developer might be suitable
Q2: Is your business a revolutionize that no others ever built? Then Software Engineer might be suitable
Hacklish
Visit: http://hacklish.appspot.com/
Backbone.js for Django Developers
Visit: http://lincolnloop.com/blog/2012/jun/5/backbonejs-django-developers/
Debug Python or Django
This will be your survival tools in python or Django.
# The classical ways import pdb; pdb.set_trace() # The cool way: Install IPython to activate this tools (pip install ipython) import ipdb; ipdb.set_trace() # For Django user, improve your shell to Shell Plus # pip install django-extensions python manage.py shell_plus # or python manage.py shell_plus --print-sql
Django Tastypie ManyToMany Through Part 2
# models.py
from django.db import models
class Ingredient(models.Model):
name = models.CharField(max_length=100)
description = models.TextField()
class RecipeIngredients(models.Model):
recipe = models.ForeignKey('Recipe')
ingredient = models.ForeignKey('Ingredient')
weight = models.IntegerField(null = True, blank = True)
class Recipe(models.Model):
title = models.CharField(max_length=100)
ingredients = models.ManyToManyField(Ingredient, through='RecipeIngredients', null = True, blank = True)
# api.py
from tastypie.resources import Resource, ModelResource
from tastypie import fields
from junk.models import RecipeIngredients, Ingredient, Recipe
class IngredientResource(ModelResource):
class Meta:
queryset = Ingredient.objects.all()
resource_name = "ingredients"
class RecipeIngredientResource(ModelResource):
ingredient = fields.ToOneField(IngredientResource, 'ingredient',full=True)
class Meta:
queryset= RecipeIngredients.objects.all()
class RecipeResource(ModelResource):
ingredients = fields.ToManyField(RecipeIngredientResource,
attribute=lambda bundle: bundle.obj.ingredients.through.objects.filter(
recipe=bundle.obj) or bundle.obj.ingredients, full=True)
class Meta:
queryset = Recipe.objects.all()
resource_name = 'recipe'
With the following query
GET /api/v1/recipe/2/?format=jsonWill return Ingredients Detail and Weight
{
"id": "2",
"ingredients": [
{
"id": "1",
"ingredient": {"description": "", "id": "2", "name": "", "resource_uri": "/api/v1/ingredients/2/"},
"recipe": "/api/v1/recipe/2/",
"resource_uri": "/api/v1/recipeingredient/1/",
"weight": null},
{
"id": "2", "ingredient": {"description": "", "id": "3", "name": "", "resource_uri": "/api/v1/ingredients/3/"},
"recipe": "/api/v1/recipe/2/",
"resource_uri": "/api/v1/recipeingredient/2/",
"weight": null}],
"resource_uri": "/api/v1/recipe/2/",
"title": ""
}
Javascript Keep Element up to date with the server data
(function keepupdate(){
doSomething();
$("#update").load('/myurl/', function(){
keepupdate();
});
})()
Compile, Link, Execute (Build and Make an Executeable File from Source Code)
Underscore Python is Private or Not?
Based on PEP8:
In python there is no Private Variable or Methods.
Most people claim that __this is actually private. But Wait:
class Car:
_gas = 1
gas = 1
car = Car()
print car.gas # result: 1
print car.gas # result: 1And Here:
class Car:
__gas = 1
car = Car()
print car._Car__gas # result: 1
In Python Nothing is really private. And there's no point of being private anyway.
Single leading Underscore Variable or methods in python convention is used for non public or only for internal use. But Of course you can call it directly from outside, It's just a convention.
And double underscore in python convention is used to avoid namespace conflict with its subclass. You can't call it directly, but to call it you have to use Class._Class__VarOrMethod
So Summary: Nothing is Private in Python