Django Foo hosting now in Russian

Posted: June 19th, 2010 | Author: Davo | Filed under: Django | Tags: | 3 Comments »

We are pleased to anounce that our hosting platform is now available in Russian.
Many thanks to Viktor “Barbuza” Kotseruba who helped with the translation.

Django Hosting


Display Django Version

Posted: June 18th, 2010 | Author: Davo | Filed under: Django | Tags: | No Comments »

Ever wondered how to detect which version of Django you are actually running?
Well it is as simple as this:

>>> import django
>>> django.VERSION
(1, 2, 1, 'final', 0)
>>> django.get_version()
u'1.2.1 SVN-13348'
>>>

Use javascript, css in admin

Posted: June 2nd, 2010 | Author: Davo | Filed under: Django | Tags: , , , | No Comments »

Ever wanted to use your own JavaScript OR CSS inside the built in Django admin?
Well… it is possible!

All you need to do is the following:
1)
Add a admin.py (if you don’t already have yet…) for your app

from django.db import models
from django.contrib import admin

from myapp.hotels.models import Hotel

class HotelAdmin(admin.ModelAdmin):
    ...
    class Media:
        js = ("/media/javascript/yourjs.js",)

admin.site.register(Hotel, HotelAdmin)

2) Create a file yourjs.js under /media/javascript

(function($) {
    $(document).ready(function($) {
         // you can now use jquery / javascript here...
         alert('It worked.');
    });
})(django.jQuery);

3) Open the admin for your application and you should see the ‘It worked’ message.


AVG Decimal simplejson (Decimal is not JSON serializable)

Posted: May 26th, 2010 | Author: ekaterina | Filed under: Django | Tags: | 1 Comment »

If you use AVG aggregation function on Integer field in MySQL,

hotels = Hotel.objects.filter(city = "Paris").extra(
    select={
        'avg_rate': 'SELECT AVG(hotel_review.rate) FROM hotel_review WHERE hotel_review.is_active = 1 AND hotel_review.hotel_id = hotel_hotel.id',
    },
)

you will have in a result { “avg_rate” : Decimal(’2.6667′)} and an “Exception Value: Decimal(’2.6667′) is not JSON serializable”.
In this case you can do this:

from django.utils import simplejson
from decimal import Decimal

class MyJSONEncoder(simplejson.JSONEncoder):
    """JSON encoder which understands decimals."""

    def default(self, obj):
        '''Convert object to JSON encodable type.'''
        if isinstance(obj, Decimal):
            return "%d" % obj
        return simplejson.JSONEncoder.default(self, obj)

hotels_json = simplejson.dumps(list(hotels.values()), cls = MyJSONEncoder)

You can read about extra search fields and JSON serialization in our article


Increment/Decrement Field value

Posted: May 26th, 2010 | Author: Davo | Filed under: Django | Tags: | No Comments »

Incrementing or decrementing field values can be done by using the F() function.
UPDATE FIELD = FIELD + 1 WHERE… ?

from django.db.models import F

# views.py ...
offer = Offer.objects.get(...)
# SQL: UPDATE field_to_increment = field_to_increment + 1 ...
offer.field_to_increment = F('field_to_increment') + 1
offer.save()